方法一:
String commandStr = command.stream().map(String::valueOf).collect(Collectors.joining(" "));
System.out.println(Thread.currentThread().getName() + "执行命令:"+commandStr);
ProcessBuilder builder = new ProcessBuilder("/bin/sh", "-c",commandStr); // /bin/sh和sh均尝试过
Process proc= null;
try {
proc = builder.start();
PipeStream out=new PipeStream(proc.getInputStream(), System.out);
PipeStream err=new PipeStream(proc.getErrorStream(), System.err);
out.start();
err.start();
proc.waitFor();
System.out.println("Exit value is: "+proc.exitValue());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
方法二:
Runtime rt = Runtime.getRuntime();
String[] cmd = new String[]{"/bin/sh","-c",command};
Process proc = rt.exec(cmd);
PipeStream out=new PipeStream(proc.getInputStream(), System.out);
PipeStream err=new PipeStream(proc.getErrorStream(), System.err);
out.start();
err.start();
proc.waitFor();
转码命令:
/usr/softinstall/ffmpeg/bin/ffmpeg -i /opt/record/22077-1629180843.2021-08-17-14:14:03.flv -vcodec copy -acodec copy /opt/record/22077-1629180843.2021-08-17-14:14:03.mp4
问题:
这条命令直接在linux中执行是执行成功的,并且会生成转码后的视频文件;
java中无论用ProcessBuilder还是Runtime.getRuntime(),也会执行成功(Process的状态码为0,且无任何报错信息),但却不能生成转码后的视频文件。
其他说明:
1.我使用的是spring框架,当在项目启动的时候在ApplicationRunner 下执行转码操作是可以执行成功并且生成转码后的视频文件的。但在controller 中调用却是不能生成文件的。
@Order(1)
@Component
public class InitPlayer implements ApplicationRunner {
@Value("${system.ffmpeg}")
private String ffmpeg;
@Override
public void run(ApplicationArguments args){
String s = FFmpeg.converMP4V2(ffmpeg,"/opt/record/22077-1629180843.2021-08-17-14:14:03.flv");
}
}
尝试过的解决办法:
1./opt/record/ 增加权限777
2./usr/softinstall/ffmpeg/bin/ffmpeg 增加权限777
以上就是我遇到的问题,总结:
在项目启动的时候正常调用转码命令并能生成转码文件,在controller 中调用也可以执行成功,但却不能生成转码后的视频文件。
帮忙看看,谢谢了。
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。