gpt4 book ai didi

java - 如何读取进程 getInputstream?

转载 作者:行者123 更新时间:2023-12-01 18:12:07 26 4
gpt4 key购买 nike

我使用进程构建器和进程(使用 ffmpeg),并且我必须将 ffmpeg 输出重定向到 java 输入。我写了这段简短的代码,但输入流无论如何都是空的......

public class Ffmpegtest {
public static void main(String[] args) {
String[] cmd = {"ffmpeg ","-i ","output2.mp4 ","-f ","mp4 ", "-"};
InputStream stream;
try {
stream = new ProcessBuilder(Arrays.asList(cmd)).start().getInputStream();
System.out.println(stream.available()); // still 0
byte[] kilo = new byte[1024];
int i = stream.read(kilo,0,1024);
while(i != -1){
System.out.println("read "+1024);
i = stream.read(kilo,0,1024);
}

} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}

最佳答案

当我运行时:

ffmpeg -i file -f mp4 -

标准输出为空。然而,stderr 说:

ffmpeg version N-74516-g99b9f01 Copyright (c) 2000-2015 the FFmpeg developers
built with Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
configuration: --enable-gpl --samples=/Users/ronaldbultje/Movies/fate-suite-ff --enable-libvpx
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 58.100 / 56. 58.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 34.100 / 5. 34.100
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Input #0, ivf, from 'file.ivf':
Duration: 00:00:00.01, start: 0.002000, bitrate: 178292 kb/s
Stream #0:0: Video: vp9 (Profile 0) (VP90 / 0x30395056), yuv420p(pc, bt470bg/unknown/unknown), 640x480, 1k tbr, 1k tbn, 1k tbc
[mp4 @ 0x7f95aa800000] muxer does not support non seekable output
Output #0, mp4, to 'pipe:':
Metadata:
encoder : Lavf56.40.101
Stream #0:0: Video: mpeg4 ( [0][0][0] / 0x0020), yuv420p, 640x480, q=2-31, 200 kb/s, 1k fps, 1k tbn, 1k tbc
Metadata:
encoder : Lavc56.58.100 mpeg4
Stream mapping:
Stream #0:0 -> #0:0 (vp9 (native) -> mpeg4 (native))
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

这里的关键是“复用器不支持不可查找的输出”。选择支持流式传输的不同输出格式(例如 mpegts),或选择文件输出而不是 stdout。作为更通用的 java 规则,在使用 Process 时始终检查 stdout 和 stderr。

关于java - 如何读取进程 getInputstream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32202783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com