【问题标题】:How to split a video file into multiple video files in Java如何在 Java 中将一个视频文件拆分为多个视频文件
【发布时间】:2014-03-22 07:11:15
【问题描述】:

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

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

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

【问题讨论】:

  • 我的示例为您的问题提供了输出。
  • 我试过了,不过我很欣赏你的代码。我想修复我的代码,因为我有 JoinVid 部分。我想问的一件事是为什么你的代码需要这么长时间才能拆分?

标签: java file split


【解决方案1】:

将文件Despicable Me 2 - Trailer (HD) - YouTube.mp4 分成二十等份

注意:将文件Despicable Me 2 - Trailer (HD) - YouTube.mp4粘贴到Documents文件夹下。

使用 java 将视频文件拆分为多个视频文件的示例。

代码:

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 中已保存视频文件的屏幕截图:

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

注意:需要的jar文件commons-io-2.2.jar

代码:

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

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

【讨论】:

  • 你也有这个的加入代码吗?顺便说一句,我注意到与其他代码相比,拆分文件所需的时间相对较长。
  • @user3449016 代码已更新。如果文件很大,则加入需要更多时间。
  • 关于join code,如果我将文件拆分为9个以上的部分,join code将从第10部分开始读取文件部分,而不是第一个部分。你能告诉我这里要解决什么问题吗?
  • @user3449016 问题已解决,代码已更新。感谢您发现问题。
  • @user3449016 代码已根据您的要求更新。
【解决方案2】:

视频分割成功但视频不播放(播放器可能不支持文件类型或可能不支持用于压缩文件的编解码器。)

package Spitvideo;

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("f:/SampleVideo_1080x720_30mb.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("d:/"+ 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 = 5;// Total files to split.
            int splitSize = inputStream.available() / totalPartsToSplit;
            int streamSize = 0;
            int read = 0;
            System.err.println("<<<<<<<<"+splitSize);
            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();
    }
}
}

【讨论】:

    【解决方案3】:

    对于特定拆分,您必须执行以下操作..

    1]计算这个文件可以创建的分区数。

    表示如果你想要 300KB 的每一部分都比...

    calculeteParts=(long)(fsize/(300));//As fsize already in KB in your program.(FOR FILE IN MB)
    

    2]从文件 f 中说,我们可以制作 30 个大小为 300KB 的分区。

    while((readInt = fin.read(b))!= -1)
            {
    
                fout.write(b, 0, readInt);
                e+= readInt;
    
              if(e==300*1024)//if Read 300 Upcoming KBs done 
                {
                    e =0L;
    
                      fout.close();
                      doPart();
    
    
                }
    
    
            }
    

    但最重要的是

    需要解决的问题

    假设您有一个大小为 9.04MB 的文件,并且您想将其划分为 300KB 的分区。 因此,以编程方式执行此操作必须使其成为 300KB 的倍数。

    示例。

    9.04*1024=9256.46KB
    So total of 9256KB approximately
    
    Now Divide it with your needed size
    9256/300=30.85 so approximately 30 parts.
    
    So 0.85 will be lost
    
    Or if you take **31**:
    
    300*31=9300 which is greater than 9256
    

    doParts(); 方法以递归方式调用,因此在 LAST PART 中可能不会达到 300KB 的大小,它将读取到可能的值,但之后它会生成 Stream Closed:IOEXception,尽管这对你来说并不重要。但没关系。

    【讨论】:

    • 谢谢,我也需要最后一部分。所以我试着把 fout.setLength(1024 * fsizeOfEachinkB); // 在这种情况下 fsizeOfEachinkB 将是 300 进入 doPart() 方法。这行得通吗?
    • 问题是,我想将每个文件拆分为 2^n(比如 512kb),然后再加入这些部分。我正在考虑将最后一部分设置回加入之前的原始大小。
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多