python - 在 python、shell 和 stdout 中使用 ffmpeg

标签 python shell ffmpeg

我想在 python 中运行以下行:

ffmpeg -i test.avi -ss 0 -r 25 -vframes 100 ./out/image-%3d.jpg 2>&1 | grep output

如果我直接在 shell 中运行它,应该输出:

>>Output #0, image2, to './out/image-%3d.jpg':

但是,当我在 python 中执行此操作时:

command = 'ffmpeg -i '+video_name+' -ss '+str(T) + ' -r '+str(25) + ' -vframes '+str(N)+' '+out_dir+'/image-%3d.jpg 2>&1 | grep output'
      argx = shlex.split(command)
      print argx
      proc = subprocess.Popen(argx,stdout=subprocess.PIPE,shell = True)
      (out,err) = proc.communicate()

它输出:

 ['ffmpeg', '-i', 'test.avi', '-ss', '0', '-r', '25', '-vframes', '100', './out/image-%3d.jpg', '2>&1', '|', 'grep', 'output']
ffmpeg version 1.2.6-7:1.2.6-1~trusty1 Copyright (c) 2000-2014 the FFmpeg developers
  built on Apr 26 2014 18:52:58 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
  configuration: --arch=amd64 --disable-stripping --enable-avresample --enable-pthreads --enable-runtime-cpudetect --extra-version='7:1.2.6-1~trusty1' --libdir=/usr/lib/x86_64-linux-gnu --prefix=/usr --enable-bzlib --enable-libdc1394 --enable-libfreetype --enable-frei0r --enable-gnutls --enable-libgsm --enable-libmp3lame --enable-librtmp --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-vaapi --enable-vdpau --enable-libvorbis --enable-libvpx --enable-zlib --enable-gpl --enable-postproc --enable-libcdio --enable-x11grab --enable-libx264 --shlibdir=/usr/lib/x86_64-linux-gnu --enable-shared --disable-static
  libavutil      52. 18.100 / 52. 18.100
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.104 / 54. 63.104
  libavdevice    53.  5.103 / 53.  5.103
  libavfilter     3. 42.103 /  3. 42.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

显然 ffmpeg 没有得到正确的参数

哪里错了?谢谢

最佳答案

shell=True时,您应该将命令作为字符串传递,而不是作为参数列表:

command = 'ffmpeg -i '+video_name+' -ss '+str(T) + ' -r '+str(25) + ' -vframes '+str(N)+' '+out_dir+'/image-%3d.jpg 2>&1 | grep output'
proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
out, err = proc.communicate()

请注意,使用 shell=Truesecurity risk if 命令 取决于用户输入。

<小时/>

如果您想使用shell=False,那么您需要replace the shell pipeline有两个 subprocess.Popen 调用,proc1.stdout 连接到 proc2.stdin:

import subprocess
PIPE = subprocess.PIPE
filename = out_dir+'/image-%3d.jpg'

args = ['ffmpeg', '-i', video_name, '-ss', T, '-r', 25, '-vframes', N, filename]
proc1 = subprocess.Popen(args, stdout=PIPE, stderr=PIPE, shell=False)
proc2 = subprocess.Popen(['grep', 'output'), stdin=proc1.stdout, stdout=PIPE, stderr=PIPE)
proc1.stdout.close()  # Allow proc1 to receive a SIGPIPE if proc2 exits.
out, err = proc2.communicate()

关于python - 在 python、shell 和 stdout 中使用 ffmpeg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25536124/

相关文章:

python - 加权元素的笛卡尔积

python - 等到 QtWidgets.QMainWindow 关闭后再继续

linux - 在没有换行的情况下用cat合并两个文件

bash - shell 脚本语法错误 : redirection unexpected during done command

java - 如何在视频开始运行之前显示视频的总时间

python - 有没有办法从 Eclipse(pyDev) 同时运行两个或多个 python 模块?

python - 将 CSV 文件的内容转换为字典

linux - 确保用户在 Debian GNU/Linux 系统上存在

image-processing - 单个位图图像的 H.264/H.265 压缩

php - FFMpeg 错误 av_interleaved_write_frame() :