gpt4 book ai didi

python - 使用 Popen 执行 ffmpeg 命令

转载 作者:太空宇宙 更新时间:2023-11-04 03:41:57 25 4
gpt4 key购买 nike

我在尝试使用 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)

最佳答案

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

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

关于python - 使用 Popen 执行 ffmpeg 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25961825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com