How to split a video file into multiple video files in Java

I started learning java coding and I want to split the video file, so here I got the code from youtube . The code broke the file into parts of the required mb. I made some modification , wanting to split the file into the required number of parts . This source code splits the file into 16 mb here:

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

therefore, if I put it, I want a value in kb, for example. 300 KB for each part, the program just does not split the file for me.

  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 

Now, if I increase the "parts" until fsizeOfEachinkB is less than 1 mb , then the program simply does not split the files at all. Can someone please help me learn this?

+7
java split file
source share
3 answers

Divide the file Despicable Me 2 - Trailer (HD) - YouTube.mp4 into twenty equal parts

Note. Paste the Despicable Me 2 - Trailer (HD) - YouTube.mp4 file into the Documents folder.

An example of splitting a video file into several video files using java.

The 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(); } } } 

Console exit:

 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 

Screenshot of saved video files in Windows 8:

Split video file using java into individual parts

Below is the code for attaching all the video files as a single video file after splitting the video into several video files.

Note: Required jar commons-io-2.2.jar file

The 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(); } } } 

Console exit:

 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

+11
source share

the video was decompressed successfully, but the video does not play (the player may not support the file type or may not support the codec that was used to compress the file.)

 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
source share

For a specific separation, you must follow these steps.

1] Calculate No from the section can be done using this file.

So, if you want each part to be 300 KB in size ...

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

2] In order to make No of Parts, say, from file f, we can make 30 patches of 300 KB in size.

 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(); } } 

But the most important thing

THE PROBLEM TO BE SOLVED

Say you have a 9.04 MB file and you want to split it into 300 KB partitions. Therefore, when programming this, you will have to make it several 300 KB.

Example.

 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(); , which is called recursively, so in a LAST PART, which CANNOT be up to 300 KB, it will read to a possible value, but after that it Stream Closed:IOEXception throw a Stream Closed:IOEXception Eventough, although this does not matter to you. p>

+2
source share

All Articles