python中使用ffmpeg自动记录检测错误

python中使用ffmpeg自动记录检测错误,python,ffmpeg,Python,Ffmpeg,我正在尝试用python运行autocrop ffmpeg命令。这个命令在Ruby中工作,所以我不知道为什么它在Python中不工作。我只是想在视频上运行一个autocrop,以防它们在顶部/底部/侧面有黑条 我不久前发现了这段代码,它在Ruby中对我起了作用,这使我认为这可能是Python语法问题,因为我在我的计算机上运行的是相同版本的FFMPEG 如果您能在这方面提供帮助,我将不胜感激 以下是Python文件中该线程的命令: 代码 import subprocess in_file = /

我正在尝试用python运行autocrop ffmpeg命令。这个命令在Ruby中工作,所以我不知道为什么它在Python中不工作。我只是想在视频上运行一个autocrop,以防它们在顶部/底部/侧面有黑条

我不久前发现了这段代码,它在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)
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
[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!
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!
经过更多的搜索,我发现了另一种方法,并一直在运行该方法,但仍然没有成功:

代码

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)
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
[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!
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!

检查行[AVFilterGraph@0x7f85ac904e20]初始化筛选器时出错。

您认为是管道shell命令的内容作为单个参数传递给ffmpeg,这会混淆它


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

如果它将来对任何人都有帮助,下面是我所面临问题的一个解决方案。我在单独的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)

你知道当我在
-vf
行中运行
-vf',crop=500:500,pad=width=550:height=550:x=25:y=25:color=white',
时它为什么会工作吗?因为这是-vf的有效参数。所有命令行工具基本上都接受字符串数组,每个字符串都可以包含空格,这是一个常见的障碍。因此
-vf
过滤器arg1:filter arg two with spaces
-vf
过滤器arg1:filter
arg
two
with
spaces