如何通过Twisted spawnProcess传递带内嵌引号的ffmpeg filter_complex参数?
问题:reactor.spawnProcess传递FFmpeg滤镜参数失败
我尝试通过reactor.spawnProcess向FFmpeg传递以下简化后的参数:
['-filter_complex', '"[0:v]setpts=PTS-STARTPTS,scale=-1:720 [a0];[a0]pad=w=1280:h=720:x=-1:y=-1:color=black[out]"']
但滤镜图始终报错,尽管在命令行输入完全相同的内容可以正常运行。我试过用repr和转义处理字符串,还是无法让参数正确传递给FFmpeg。
补充信息
- 我写了一个小脚本接收输入参数,参数本身是正常的,但无法理解为什么用
" ".join(params)在终端能运行,用spawnProcess就失败。 - FFmpeg输出的错误信息:
Splitting the commandline. Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'. Reading option '-v' ... matched as option 'v' (set logging level) with argument 'verbose'. Reading option '-threads' ... matched as AVOption 'threads' with argument 'auto'. Reading option '-flags' ... matched as AVOption 'flags' with argument '-global_header'. Reading option '-i' ... matched as input url with argument 'config/colorbars.jpg'. Reading option '-c:v' ... matched as option 'c' (codec name) with argument 'copy'. Reading option '-tag:v' ... matched as option 'tag' (force codec tag/fourcc) with argument 'hvc1'. Reading option '-filter_complex' ... matched as option 'filter_complex' (create a complex filtergraph) with argument '"[0:v]setpts=(RTCTIME-RTCSTART)/(TB*1000000),scale=-1:720[outpad];[outpad]pad=w=1280:h=720:x=-1:y=-1:color=black[out]"'. Reading option '-map' ... matched as option 'map' (set input stream mapping) with argument '"[out]"'. Reading option '-c:v' ... matched as option 'c' (codec name) with argument 'hevc_videotoolbox'. Reading option '-b:v' ... matched as option 'b' (video bitrate (please use -b:v)) with argument '3000K'. Reading option '-preset' ... matched as AVOption 'preset' with argument 'medium'. Reading option '-profile:v' ... matched as option 'profile' (set profile) with argument 'main'. Reading option '-pix_fmt' ... matched as option 'pix_fmt' (set pixel format) with argument 'yuv420p'. Reading option '-t' ... matched as option 't' (record or transcode "duration" seconds of audio/video) with argument '5'. Reading option 'temp/apptest_1.mp4' ... matched as output url. Reading option '-y' ... matched as option 'y' (overwrite output files) with argument '1'. Finished splitting the commandline. Parsing a group of options: global . Applying option loglevel (set logging level) with argument debug. Applying option v (set logging level) with argument verbose. Input #0, image2, from 'config/colorbars.jpg': Duration: 00:00:00.04, start: 0.000000, bitrate: 4712 kb/s Stream #0:0: Video: mjpeg (Baseline), 1 reference frame, yuvj420p(pc, bt470bg/unknown/unknown, center), 1280x720 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 25 tbn [AVFilterGraph @ 0x600001fbc600] No such filter: '"' Error initializing complex filters. Invalid argument
解决方案
问题根源在于滤镜参数的双引号。FFmpeg维基示例里会在滤镜图外加双引号,但通过reactor.spawnProcess传递参数时不需要添加双引号。
修正后的参数列表:
['-filter_complex', '[0:v]setpts=PTS-STARTPTS,scale=-1:720 [a0];[a0]pad=w=1280:h=720:x=-1:y=-1:color=black[out]']
原因是命令行中的双引号是给shell用来分割参数的,而spawnProcess直接传递参数列表,无需shell做解析,所以原参数中的双引号会被FFmpeg当成滤镜图的一部分,进而触发No such filter: '"'错误。
内容的提问来源于stack exchange,提问作者Stephen
相关产品推荐
相关产品推荐

