【问题标题】:not able to merge 2 video streams into one file [duplicate]无法将 2 个视频流合并到一个文件中 [重复]
【发布时间】:2019-12-18 19:58:21
【问题描述】:

谁能告诉我下面的代码有什么问题,我正在尝试将两个不同的视频 URL 合并到同一个文件中,(两个视频的大小相同 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();
}

我尝试使用上述两种方法保存文件,但都只创建一个视频文件,即不附加第二个视频(如果给定不同的名称,我可以保存两个文件)

【问题讨论】:

  • 我认为你正在反对标题 - “只创建一个视频”意味着你只播放一个视频 - 如果你提供一个完整的程序,我可能会回答

标签: java inputstream bufferedinputstream


【解决方案1】:

要连接两个视频,您需要特殊的软件。 ffmpeg 是一个:

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

如果您想播放合并后的视频。如果您只需要存储信息,您通常的方式应该可以工作。

【讨论】:

  • 我一直在使用 ffmpeg,但最近在处理来自不同来源的不同参数的视频时,我更经常收到非单调数据错误 - video.stackexchange.com/questions/15468/…,这就是为什么我想尝试这种使用 Java 合并视频的方式。
  • 如您所见,您无法做到 - java中有一些视频处理库 - 我不知道他们是否可以即时完成。也许你可以试试这个新的 concat 过滤器,看看它是否有效。
【解决方案2】:

问题是替换文件的内容而不是连接。 函数 FileOutputStream(File file, boolean append) 为此目的使用第二个参数。将此方法与第二个参数的 true 值一起使用

【讨论】:

  • 是的,我试过那个方法。如果您看到注释代码,我尝试了该方法,但它没有合并文件。只有第一个文件被保存。
  • @ArigatoManga 如果只是新建一个文件输出流并使用该输出流写入两个输入流,会发生什么?
  • 只附加第一个流,不附加第二个流。
猜你喜欢
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多