Java调用FFmpeg在Linux环境下无响应无报错问题排查
解决Linux下Java调用FFmpeg无反应(waitFor()返回-1)的问题
问题现象
开发的播客下载器需调用FFmpeg处理下载后的音频文件,Windows环境运行正常,但Linux环境下调用FFmpeg时出现异常:
process.waitFor()返回值为-1- Java日志无报错信息,
ffmpeg.log为空 - 未生成目标处理文件
- 手动在Linux命令行执行相同命令可正常完成处理
已排除FFmpeg权限问题(权限为rwxr-xr-x),且验证FFmpeg路径可被正确定位(修改路径为大写时触发"找不到文件"错误)。
问题代码与错误参数
Java调用代码
ProcessBuilder processBuilder = new ProcessBuilder( commands ); processBuilder.redirectErrorStream( true ); // Added the next line to see if I could log an error from ffmpeg processBuilder.redirectOutput( ProcessBuilder.Redirect.appendTo( new File( "./ffmpeg.log" ) ) ); Process process = processBuilder.start(); BufferedReader bufferedOutputReader = new BufferedReader( new InputStreamReader( process.getInputStream() ) ); do { outputString = bufferedOutputReader.readLine(); if ( outputString != null ) { LOGGER.trace( outputString ); } } while ( outputString != null ); results = process.waitFor(); LOGGER.debug( "Exit value: " + results );
错误的命令参数(含多余双引号)
2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: /bin/ffmpeg 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: -y 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: -i 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: "/home/todd/aggregator/incoming/Downrange Radio/Downrange_Radio_2023-02-15_The_Avidity_PD-10_Delivers_.mp3" 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: -ac 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: 1 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: -af 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: "atempo=1.4,volume=1.4" 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: "/home/todd/aggregator/processed/Downrange Radio/Downrange_Radio_2023-02-15_The_Avidity_PD-10_Delivers_.mp3"
根因分析
命令行中使用双引号是为了告诉shell:带空格的字符串是一个完整的参数,但ProcessBuilder是直接将参数数组传递给目标进程,不需要依赖shell解析。手动给参数添加双引号后,这些引号会被当成参数内容的一部分,导致FFmpeg无法识别带引号的路径和滤镜参数,进而无法正常启动处理流程。
解决方案
移除所有参数中手动添加的双引号,直接传递原始路径和参数值:
修正后的参数示例
2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: /bin/ffmpeg 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: -y 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: -i 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: /home/todd/aggregator/incoming/Downrange Radio/Downrange_Radio_2023-02-15_The_Avidity_PD-10_Delivers_.mp3 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: -ac 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: 1 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: -af 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: atempo=1.4,volume=1.4 2023-10-25 16:21:52,452 224257 [pool-1-thread-4] DEBUG org.sperbolink.utils.CodecUtils - command string: /home/todd/aggregator/processed/Downrange Radio/Downrange_Radio_2023-02-15_The_Avidity_PD-10_Delivers_.mp3
ProcessBuilder会自动将带空格的路径作为单个参数传递给FFmpeg,效果与命令行手动执行一致。
内容的提问来源于stack exchange,提问作者Todd
相关产品推荐
相关产品推荐

