【问题标题】:Executing ffmpeg command using Popen使用 Popen 执行 ffmpeg 命令
【发布时间】:2014-11-15 16:41:55
【问题描述】:

我在尝试使用 Popen 执行 ffmpeg 命令时遇到了一个奇怪的问题。 我有以下代码,用于在 Python 中执行外部命令:

from subprocess import Popen, PIPE
from datetime import datetime


class Executor(object):

    @classmethod
    def execute(cls, command):
        """
        Executing a given command and
        writing into a log file in cases where errors arise.
        """
        p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
        output, err = p.communicate()
        if p.returncode:
            with open("failed_commands.log", 'a') as log:
                now = datetime.now()
                log.write('{}/{}/{} , {}:{}:{}\n\n'.format(now.day, now.month,
                                                           now.year, now.hour,
                                                           now.minute,
                                                           now.second))

                log.write("COMMAND:\n{}\n\n".format(" ".join(command)))
                log.write("OUTPUT:\n{}\n\n".format(output.decode("utf-8")))
                log.write("ERRORS:\n{}\n".format(err.decode("utf-8")))
                log.write('-'*40)
                log.write('\n')

            return ''

        if not output:
            output += ' '

        return output

我已经用其他命令对其进行了测试,但是当我尝试执行 ffmpeg 命令时 - 它失败了。 我正在尝试将一些音频格式转换为 mp3 格式。 这是我的命令示例:

ffmpeg -i "/path/old_song.m4a" "/path/new_song.mp3"

...就这么简单。当我在终端中运行它时,它工作正常,但是当我尝试使用上述函数执行它时,它失败了。 这是确切的错误:

----------------------------------------
21/9/2014 , 19:48:50

COMMAND:
ffmpeg -i "/path/old_song.m4a" "/path/new_song.mp3"

OUTPUT:


ERRORS:
ffmpeg version 2.2.3 Copyright (c) 2000-2014 the FFmpeg developers
  built on Jun  9 2014 08:01:43 with gcc 4.9.0 (GCC) 20140521 (prerelease)
  configuration: --prefix=/usr --disable-debug --disable-static --enable-avisynth --enable-avresample --enable-dxva2 --enable-fontconfig --enable-gnutls --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libv4l2 --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-pic --enable-postproc --enable-runtime-cpudetect --enable-shared --enable-swresample --enable-vdpau --enable-version3 --enable-x11grab
  libavutil      52. 66.100 / 52. 66.100
  libavcodec     55. 52.102 / 55. 52.102
  libavformat    55. 33.100 / 55. 33.100
  libavdevice    55. 10.100 / 55. 10.100
  libavfilter     4.  2.100 /  4.  2.100
  libavresample   1.  2.  0 /  1.  2.  0
  libswscale      2.  5.102 /  2.  5.102
  libswresample   0. 18.100 /  0. 18.100
  libpostproc    52.  3.100 / 52.  3.100
"/path/old_song.m4a": No such file or directory
Conversion failed!

----------------------------------------

...如你所想 - 文件存在。

我认为将命令传递给Popen.communicate 是有问题的,但我不知道具体情况。

亲切的问候,

特奥多 D. PS:我将命令传递给Executor.execute as Python 列表。

PSS:拨打Executor.execute

def process_conversion(self):
    for song in self.files_to_convert:
        current_format = song.rsplit('.', 1)[-1]

        old_file = '"{}{}{}"'.format(self.target_dir, os.sep, song)
        new_file = '"{}{}{}"'.format(self.target_dir, os.sep,
                                     song.replace(current_format, 'mp3'))

        command = ["ffmpeg", "-i", old_file, new_file]
        Executor.execute(command)

【问题讨论】:

  • 您遗漏了一条关键信息:产生该输出的对 Executor.execute 的确切调用。
  • 此输出来自 failed_commands.log 文件。换句话说,输出在“ERRORS:”之后。

标签: python ffmpeg popen python-3.4


【解决方案1】:

问题是您在文件名中包含双引号。改用这个:

    old_file = '{}{}{}'.format(self.target_dir, os.sep, song)

【讨论】:

  • 我可以发誓我已经尝试过了,但没有成功。但我想在那之后我已经更改了其他内容。只是为了让我更清楚 - 双引号仅在终端使用时才需要to 告诉解释器以下内容是单个参数(如果它包含空格)?
  • 正确; shell 使用空格来分隔字符串中的参数;引号保护封闭空间。 Python 不必将单个字符串解析为参数列表。您只需使用 list 对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 2020-10-11
  • 2021-09-09
相关资源
最近更新 更多