python子进程中的ffmpeg-无法为'pipe:'找到合适的输出格式 [英] ffmpeg in Python subprocess - Unable to find a suitable output format for 'pipe:'

查看:438
本文介绍了python子进程中的ffmpeg-无法为'pipe:'找到合适的输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用ffmpeg通过Python将字幕刻录到视频中.在命令行中工作正常,但是在使用以下命令从Python子进程调用时:

Trying to burn subs into video with ffmpeg via Python. Works fine in the command line, but when calling from Python subprocess with:

p = subprocess.Popen('cd ~/Downloads/yt/; ffmpeg -i ./{video} -vf subtitles=./{subtitles} {out}.mp4'.format(video=vid.replace(' ', '\ '), subtitles=subs, out='out.mp4'), shell=True)

我得到:

Unable to find a suitable output format for 'pipe:'

完整追溯:

'ffmpeg version 2.7.2 Copyright (c) 2000-2015 the FFmpeg developers
  built with Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.7.2_1 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-libfreetype --enable-libvpx --enable-libass --enable-libfdk-aac --enable-nonfree --enable-vda
  libavutil      54. 27.100 / 54. 27.100
  libavcodec     56. 41.100 / 56. 41.100
  libavformat    56. 36.100 / 56. 36.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 16.101 /  5. 16.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.100 /  1.  2.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from './OnHub - a router for the new way to Wi-Fi-HNnfHP7VDP8.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.36.100
  Duration: 00:00:53.94, start: 0.000000, bitrate: 2092 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 1961 kb/s, 23.98 fps, 23.98 tbr, 90k tbn, 47.95 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
[NULL @ 0x7fc07b077600] Unable to find a suitable output format for 'pipe:'
pipe:: Invalid argument'

推荐答案

我猜测问题是您在某些参数中有空格,这些空格是您无法逃避的.您可以逃脱它,但这是做您想做的更好的方法:

I'm guessing the problem was that you had spaces in some argument that you weren't escaping. You could just escape it, but this is a better way to do what you're trying to do:

import os


directory_path = os.path.expanduser('~/Downloads/yt/')
video_path = 'path/to/video'
subtitles_path = 'path/to/subtitles'
outfile_path = 'out.mp4'

args = ['ffmpeg', '-i', video_path, '-vf',
        'subtitles={}'.format(subtitles_path), outfile_path]]
p = subprocess.Popen(args, cwd=directory_path)

主要区别是您没有使用shell=True,出于安全性和其他原因,这是一个好习惯,其中包括您不必担心用引号引起来的事实.由于未使用shell=True,因此您必须在命令行中将其作为字符串列表传递,每个参数一个元素,而不是以前的一个字符串.

The main difference is that you're not using shell=True, which is good practice for security and other reasons, including the fact that you don't have to worry about quoting arguments with spaces. Because it's not using shell=True, you have to pass in the command line as a list of strings, one element per argument, instead of one string like before.

这篇关于python子进程中的ffmpeg-无法为'pipe:'找到合适的输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆