在python,shell和stdout中使用ffmepg

编码器

我想在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=True安全风险,如果command依赖于用户输入。


如果您想使用shell=False,那么您需要用两个调用替换shell管道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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在处理和python中使用kinect

来自分类Dev

无法在Python Shell中使用Tab

来自分类Dev

在FLASK和python中使用mongo

来自分类Dev

Shell脚本:在shebang中使用grep和tr

来自分类Dev

在Python中使用[]和list()之间的区别

来自分类Dev

using ffmepg in python, shell and stdout

来自分类Dev

在Python和wxPython中使用“自我”

来自分类Dev

在Selenium和Python中使用Faker

来自分类Dev

在python中使用for循环和嵌套列表

来自分类Dev

在Bash中使用Python Shell

来自分类Dev

无法在Python Shell中使用Tab

来自分类Dev

在gdb中使用STDOUT

来自分类Dev

如何使用subprocess.call从Python Shell exec拦截stdout?

来自分类Dev

在nc连接中使用来自管道命令的stdout和括号中的命令

来自分类Dev

什么是“校验和”?如何从shell中使用它?

来自分类Dev

在脚本中使用sudo su运行命令,并重定向stdin和stdout

来自分类Dev

在Shell脚本中使用grep和if语句

来自分类Dev

在shell脚本中使用'*'

来自分类Dev

在awk脚本中使用if和shell变量

来自分类Dev

在BeautifulSoup和Python中使用SVG路径

来自分类Dev

在python中使用for循环和嵌套列表

来自分类Dev

在Python中使用管道执行Shell命令

来自分类Dev

在Python中使用开放函数和循环

来自分类Dev

在ansible shell命令中使用{{和}}

来自分类Dev

在 Python 中使用 g++ 和 subprocess

来自分类Dev

在 Linux 中使用已编译的 Python shell

来自分类Dev

Ansible - 在 JSON 和 shell 中使用外部变量

来自分类Dev

什么时候在 Jenkins 中使用 Groovy 和 shell 脚本?

来自分类Dev

在 awk 脚本中使用管道和 shell 命令