Java implements splitting and merging of video files

package DeliverFile;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
 
public class DeliverFile2 {
 
    public static void main(String[] args) {
        cut();
        merge();
    }
//split file
    public static void cut() {
        File file = new File("G:\\test\\source.avi");
        int num = 10;//Number of split files
 
        long lon = file.length() / 10L + 1L;//Make the number of bytes in the file +1, to ensure that all bytes are obtained
        try {
            RandomAccessFile raf1 = new RandomAccessFile(file, "r");
 
            byte[] bytes = new byte [1024];//The smaller the value is set, the closer the number of bytes of each file is to the average, but the efficiency will be reduced, here is a compromise, take 1024
            int len ​​= -1;
            for (int i = 0; i < 10; i++) {
                String name = "G:\\test2\\source" + i + ".avi";
                File file2 = new File(name);
                RandomAccessFile raf2 = new RandomAccessFile(file2, "rw");
 
                while ((len = raf1.read(bytes)) != -1){//When the end of the file is read, len returns -1, ending the loop
                    raf2.write( bytes, 0, len);
                    if (raf2.length() > lon)//When the number of bytes of the new file generated is greater than lon, end the loop
                        break;
                }
                raf2.close();
            }
            raf1.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
//合并文件
    public static void merge() {
        File file = new File("G:\\test2\\new.avi");
        try {
            RandomAccessFile target = new RandomAccessFile(file, "rw");
            for (int i = 0; i < 10; i++) {
                File file2 = new File("G:\\test2\\source" + i + ".avi");
                RandomAccessFile src = new RandomAccessFile(file2, "r");
                byte[] bytes = new byte[1024];//每次读取字节数
                int len = -1;
                while ((len = src.read(bytes)) != -1) {
                    target.write(bytes, 0, len);//loop assignment
                }
                src.close();
            }
            target.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325684716&siteId=291194637