Java中如何将一个视频文件分割成多个视频文件

2024-01-07

我开始学习java编码,我想分割视频文件,所以在这里我获取了代码youtube。代码将文件拆分为所需的 mb 部分我做了一些修改希望它将文件分割成所需数量的部分。这段原始代码将文件分割成16 mb here :

if(e==1024*1024*16) // split the file to 16 mb for each part
        {
            e =0L;
            fout.close();
            doPart();
        }

所以如果我把想要的值以 kb 为单位,例如。每个部分 300kb,该程序只是不为我分割文件。

    package fsplit;

    import java.io.File;
    import java.io.RandomAccessFile;

    public class SplitVid {
    public static void main(String args[])
    {
        try {
            new SplitVid();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } // end main
    File f=new File("thisfile.mp4");
    long fsize = f.length()/1024; // file size in bytes
    long parts = 9; // Divide the file into how many parts?
    long fsizeOfEachinkB = fsize/parts;
    int readInt;
    RandomAccessFile fin, fout;
    byte b[] = new byte[2048];
    long e = 0L;
    int j = 1;
    public SplitVid() throws Exception
    {
        fin=new RandomAccessFile(f, "r");
        doPart();

    }

    public void doPart() throws Exception
    {
        fout = new RandomAccessFile(f.getPath() + "Part"+j++, "rw");

        while((readInt = fin.read(b))!= -1)
        {
            fout.write(b, 0, readInt);
            e+= readInt;


            **if(e==1024*fsizeOfEachinkB)//divide each file into fsize/parts per file**
            {
                e =0L;
                fout.close();
                doPart();
            }
        }
        System.out.println("The size of this file is " + f.length()/1024 + " kb");
        System.out.println("The file is divided into " + parts + " parts");
        System.out.println("Each part has " + fsizeOfEachinkB + " kb");
        fout.close();
        fin.close();
        f.delete(); // deletes the original file after the split is done

    }


} //end class

现在如果我增加“零件”直到fsizeOfEachinkB小于1 mb,那么程序就根本不分割文件。有人请帮我调查一下吗?


分割文件神偷奶爸 2 - 预告片 (HD) - YouTube.mp4 https://app.box.com/s/rstiu9t2veubyyzbn17n分成二十等份

Note:粘贴文件Despicable Me 2 - Trailer (HD) - YouTube.mp4 under Documents folder.

使用java将视频文件分割成多个视频文件的示例。

Code:

package com.uk.mysqlmaven.jsf.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * 
 * @author uday.p
 *
 */
public class SplitVideoFile {
    public static void main(String[] args) {
        try {
            File file = new File("C:/Documents/Despicable Me 2 - Trailer (HD) - YouTube.mp4");//File read from Source folder to Split.
            if (file.exists()) {

            String videoFileName = file.getName().substring(0, file.getName().lastIndexOf(".")); // Name of the videoFile without extension
            File splitFile = new File("C:/Documents/Videos_Split/"+ videoFileName);//Destination folder to save.
            if (!splitFile.exists()) {
                splitFile.mkdirs();
                System.out.println("Directory Created -> "+ splitFile.getAbsolutePath());
            }

            int i = 01;// Files count starts from 1
            InputStream inputStream = new FileInputStream(file);
            String videoFile = splitFile.getAbsolutePath() +"/"+ String.format("%02d", i) +"_"+ file.getName();// Location to save the files which are Split from the original file.
            OutputStream outputStream = new FileOutputStream(videoFile);
            System.out.println("File Created Location: "+ videoFile);
            int totalPartsToSplit = 20;// Total files to split.
            int splitSize = inputStream.available() / totalPartsToSplit;
            int streamSize = 0;
            int read = 0;
            while ((read = inputStream.read()) != -1) {

                if (splitSize == streamSize) {
                    if (i != totalPartsToSplit) {
                        i++;
                        String fileCount = String.format("%02d", i); // output will be 1 is 01, 2 is 02
                        videoFile = splitFile.getAbsolutePath() +"/"+ fileCount +"_"+ file.getName();
                        outputStream = new FileOutputStream(videoFile);
                        System.out.println("File Created Location: "+ videoFile);
                        streamSize = 0;
                    }
                }
                outputStream.write(read);
                streamSize++;
            }

            inputStream.close();
            outputStream.close();
            System.out.println("Total files Split ->"+ totalPartsToSplit);
        } else {
            System.err.println(file.getAbsolutePath() +" File Not Found.");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

控制台输出:

File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/01_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/02_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/03_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/04_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/05_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/06_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/07_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/08_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/09_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/10_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/11_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/12_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/13_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/14_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/15_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/16_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/17_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/18_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/19_Despicable Me 2 - Trailer (HD) - YouTube.mp4
File Created Location: C:\Documents\Videos_Split\Despicable Me 2 - Trailer (HD) - YouTube/20_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Total files Split ->20

Windows 8 中保存的视频文件的屏幕截图:

以下是将视频分割为多个视频文件后将所有视频文件合并为单个视频文件的代码。

Note:所需的 jar 文件commons-io-2.2.jar https://app.box.com/s/363ge6r24oplasj4nnqp

Code:

package com.uk.mysqlmaven.jsf.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
/**
 * 
 * @author uday.p
 *
 */
public class JoinVideoFile {
    public static void main(String[] args) {
        try {
            File splitFiles = new File("C:/Documents/Videos_Split/Despicable Me 2 - Trailer (HD) - YouTube/");// get all files which are to be join
            if (splitFiles.exists()) {
                File[] files = splitFiles.getAbsoluteFile().listFiles();
                if (files.length != 0) {
                    System.out.println("Total files to be join: "+ files.length);

                String joinFileName = Arrays.asList(files).get(0).getName();
                System.out.println("Join file created with name -> "+ joinFileName);

                String fileName = joinFileName.substring(0, joinFileName.lastIndexOf("."));// video fileName without extension
                File fileJoinPath = new File("C:/Documents/Videos_Join/"+ fileName);// merge video files saved in this location

                if (!fileJoinPath.exists()) {
                    fileJoinPath.mkdirs();
                    System.out.println("Created Directory -> "+ fileJoinPath.getAbsolutePath());
                }

                OutputStream outputStream = new FileOutputStream(fileJoinPath.getAbsolutePath() +"/"+ joinFileName);

                for (File file : files) {
                    System.out.println("Reading the file -> "+ file.getName());
                    InputStream inputStream = new FileInputStream(file);

                    int readByte = 0;
                    while((readByte = inputStream.read()) != -1) {
                        outputStream.write(readByte);
                    }
                    inputStream.close();
                }

                System.out.println("Join file saved at -> "+ fileJoinPath.getAbsolutePath() +"/"+ joinFileName);
                outputStream.close();
            } else {
                System.err.println("No Files exist in path -> "+ splitFiles.getAbsolutePath());
            }
        } else {
            System.err.println("This path doesn't exist -> "+ splitFiles.getAbsolutePath());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

控制台输出:

Total files to be join: 20
Join file created with name -> 01_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 01_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 02_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 03_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 04_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 05_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 06_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 07_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 08_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 09_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 10_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 11_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 12_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 13_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 14_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 15_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 16_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 17_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 18_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 19_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Reading the file -> 20_Despicable Me 2 - Trailer (HD) - YouTube.mp4
Join file saved at -> C:\Documents\Videos_Join\01_Despicable Me 2 - Trailer (HD) - YouTube/01_Despicable Me 2 - Trailer (HD) - YouTube.mp4

Screenshot of Join Video file saved in Windows 8: Join file screenshot

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java中如何将一个视频文件分割成多个视频文件 的相关文章

随机推荐