0

Could someone please tell me what is wrong in the below code, I'm trying to merge two different video URLs to same file, (both videos have the same size 1024x720)

String url1 = "https://test.com/vid1";
String url2 = "https://test.com/vid2";

FileOutputStream out = new FileOutputStream(new File("test.mp4"));
writeToFile(url1, out);
writeToFile(url2, out);
out.close();

//Even tried the below way of first saving one file and then opening the same file to append the stream data
/*
FileOutputStream out = new FileOutputStream(new File("test.mp4"));
writeToFile(url1, out);
out.close();

out = new FileOutputStream(new File("test.mp4"), true);
writeToFile(url2, out);
out.close();
*/

void writeToFile(String url, FileOutputStream out) {
    HttpsURLConnection con = (HttpsURLConnection) new URL(url).openConnection();
    con.setRequestMethod("GET");

    BufferedInputStream bis = new BufferedInputStream(con.getInputStream());

    int count;
    byte buf[] = new byte[20480];

    while((count = bis.read(buf, 0, 20480)) != -1)
        out.write(buf, 0, count);

    bis.close();
    con.disconnect();
}

I have tried to save the file using the above two methods but both create only one video file i.e., the second video is not appended (i'm able to save both files if given different names)

ArigatoManga
  • 594
  • 5
  • 23
  • I think you are running against headers - by "creating only one video" you mean you play only one video - if you give a complete program I might answer – gpasch Aug 13 '19 at 08:49

2 Answers2

0

The problem is replacing the content of file and not concat. the function FileOutputStream(File file, boolean append) use second parameter for this purpose. use this method with true value for the second parameter

  • yes, i tried that method. If you see the commented code, i tried that method but it did not merge the files.only the first file is being saved. – ArigatoManga Aug 12 '19 at 08:33
  • @ArigatoManga what occurs if just, new one file output stream with true appending and write two input streams with that output stream ? –  Aug 12 '19 at 08:37
  • only first stream is appended second stream is not appended. – ArigatoManga Aug 12 '19 at 08:39
0

To concatenate two videos you need special software. ffmpeg is one:

ffmpeg -i vid-1.mp4 -i vid-2.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" all.mp4

If you want to play the combined video. If you only need to store the info, your usual way should work.

gpasch
  • 2,672
  • 3
  • 10
  • 12
  • I have been using ffmpeg from a long time but recently when working with videos from different sources with different parameters I am getting non-monotonous data error more often - https://video.stackexchange.com/questions/15468/non-monotonous-dts-on-concat-ffmpeg , that is the reason why I wanted to try this way of merging videos using Java. – ArigatoManga Aug 13 '19 at 13:05
  • As you can see you cannot do it - there are some video processing libraries in java - I dont know if they can do it on the fly. Maybe you can try this newer concat filter also see if it works. – gpasch Aug 13 '19 at 13:20