【问题标题】:Autocrop detect error with ffmpeg in python在python中使用ffmpeg自动裁剪检测错误
【发布时间】:2017-10-03 01:30:43
【问题描述】:

我正在尝试在 python 中运行 autocrop ffmpeg 命令。该命令在 Ruby 中有效,所以我不确定为什么它在 Python 中无效。我只是想对视频进行自动裁剪,以防它们在顶部/底部/侧面有黑条。

我不久前发现了这段代码from this thread,它在 Ruby 中对我有用,这让我认为这可能是 Python 语法问题,因为我在我的计算机上运行相同版本的 FFMPEG。

希望能对此提供任何帮助

这是我的 Python 文件中该线程的命令:

代码

import subprocess

in_file = /path/input.mp4
out_file = /path/output.mp4

cmd = [
    'ffmpeg', 
    '-y',
    '-i', in_file,
    '-vf', 'cropdetect -f null - 2>&1 | awk \'/crop/ { print \$NF }\' | tail -1',
    out_file
]
subprocess.Popen(cmd, stdout = subprocess.PIPE, bufsize=10**8)

错误

ffmpeg version 3.3.4 Copyright (c) 2000-2017 the FFmpeg developers
  built with Apple LLVM version 7.0.2 (clang-700.1.81)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma --enable-nonfree --enable-vda
  libavutil      55. 58.100 / 55. 58.100
 [...]
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[AVFilterGraph @ 0x7f8c23d1dc60] No such filter: 'cropdetect -f null - 2>&1 | awk /crop/ { print $NF } | tail -1'
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
[aac @ 0x7f8c2400a400] Qavg: 45917.641
[aac @ 0x7f8c2400a400] 2 frames left in the queue on closing
Conversion failed!

经过更多搜索,我发现了另一种方法,并且一直在努力运行它,但仍然没有运气:

代码

import subprocess

in_file = /path/input.mp4
out_file = /path/output.mp4

cmd = [
    'ffmpeg', 
    '-y',
    '-i', in_file,
    '-vf', 'cropdetect=24:16:0 -y -crf 51 ultrafast -f null /dev/null 2>&1 | grep -o crop=.* | sort -bh | uniq -c | sort -bh | tail -n1 | grep -o crop=.*', 
    out_file
]
subprocess.Popen(cmd, stdout = subprocess.PIPE, bufsize=10**8)

错误

ffmpeg version 3.3.4 Copyright (c) 2000-2017 the FFmpeg developers
  built with Apple LLVM version 7.0.2 (clang-700.1.81)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma --enable-nonfree --enable-vda
  libavutil      55. 58.100 / 55. 58.100
[ ... ]
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[01/Oct/2017 16:44:25] "GET /home/ HTTP/1.1" 200 13844
[cropdetect @ 0x7f85aca02d00] [Eval @ 0x7fff5cfc1820] Undefined constant or missing '(' in 'y-crf51ultrafast-fnull/dev/null2>&1|grep-ocrop=.*|sort-bh|uniq-c|sort-bh|tail-n1|grep-ocrop=.*'
[cropdetect @ 0x7f85aca02d00] Unable to parse option value "0 -y -crf 51 ultrafast -f null /dev/null 2>&1 | grep -o crop=.* | sort -bh | uniq -c | sort -bh | tail -n1 | grep -o crop=.*"
[cropdetect @ 0x7f85aca02d00] [Eval @ 0x7fff5cfc18a0] Undefined constant or missing '(' in 'y-crf51ultrafast-fnull/dev/null2>&1|grep-ocrop=.*|sort-bh|uniq-c|sort-bh|tail-n1|grep-ocrop=.*'
[cropdetect @ 0x7f85aca02d00] Unable to parse option value "0 -y -crf 51 ultrafast -f null /dev/null 2>&1 | grep -o crop=.* | sort -bh | uniq -c | sort -bh | tail -n1 | grep -o crop=.*"
[cropdetect @ 0x7f85aca02d00] Error setting option reset to value 0 -y -crf 51 ultrafast -f null /dev/null 2>&1 | grep -o crop=.* | sort -bh | uniq -c | sort -bh | tail -n1 | grep -o crop=.*.
[Parsed_cropdetect_0 @ 0x7f85aca02c40] Error applying options to the filter.
[AVFilterGraph @ 0x7f85ac904e20] Error initializing filter 'cropdetect' with args '24:16:0 -y -crf 51 ultrafast -f null /dev/null 2>&1 | grep -o crop=.* | sort -bh | uniq -c | sort -bh | tail -n1 | grep -o crop=.*'
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
[aac @ 0x7f85ad805000] Qavg: 45917.641
[aac @ 0x7f85ad805000] 2 frames left in the queue on closing
Conversion failed!

【问题讨论】:

    标签: python ffmpeg


    【解决方案1】:

    查看[AVFilterGraph @ 0x7f85ac904e20] Error initializing filter ..这一行

    你认为的管道 shell 命令作为单个参数传递给 ffmpeg,这让它感到困惑。

    在运行命令之前添加一个print(cmd),您会看到“-vf”标志只是获得了一个非常大的参数。

    【讨论】:

    • 你知道为什么当我在-vf 行中运行'-vf', 'crop=500:500, pad=width=550:height=550:x=25:y=25:color=white', 时它会起作用
    • 因为这是 -vf 的有效参数。所有命令行工具基本上都接受一个字符串数组,每个字符串都可以包含空格——这是一个常见的绊脚石。所以-vffilter_arg1:filter arg two with spaces-vffilter_arg1:filterargtwowithspaces 是根本不同的
    【解决方案2】:

    如果它对将来的任何人有所帮助,这是我所面临问题的一​​种解决方案。我在单独的 ffmpeg 命令中运行cropdetect,并将其输出存储为变量。

    我对python不太熟悉,所以这个解决方案可能不是最优雅的,如果有更多python经验的人有更好的格式化子流程布局的方法,看到更好的布局会很棒

    我对此进行了测试,它可以工作:

    import subprocess
    
    in_file = '/path/input.mp4'
    out_file = '/path/output.mp4'
    
    crop_dimensions = subprocess.Popen(['ffmpeg -i ' + in_file + ' -vf cropdetect -f null - 2>&1 | awk \'/crop/ { print $NF }\' | tail -1'], shell=True, stdout=subprocess.PIPE).stdout.read().strip()
    
    cmd = [
        'ffmpeg', 
        '-y',
        '-i', in_file,
        '-vf', crop_dimensions,
        out_file
    ]
    subprocess.Popen(cmd, stdout = subprocess.PIPE, bufsize=10**8)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2020-11-02
      • 2018-06-10
      • 2012-12-22
      相关资源
      最近更新 更多