如何在Python中使用ffmpeg压缩视频?

问题描述 投票:0回答:2

我用 Python 3 编写了这个用于压缩视频的脚本:

import os
import sys
import subprocess
result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4')
print(result)

当我运行上面的代码时,会出现一些错误,例如

System cannot find the file specified

  result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4')
  File "C:\Program Files\Python37\lib\subprocess.py", line 488, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Program Files\Python37\lib\subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files\Python37\lib\subprocess.py", line 1207, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

问题:如何修复代码以正确压缩视频?

python python-3.x ffmpeg windows-10 video-compression
2个回答
3
投票

几乎正确!看起来您需要的唯一模块是 subprocess。您应该在

run()
函数中运行命令。试试这个:

import subprocess
result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4')
print(result)

0
投票

尝试将您的命令作为列表传递给

subprocess

import os
import sys
import subprocess
result = subprocess.run(['ffmpeg', '-i', 'output.mp4', '-b', '800k', 'output.mp4'])
print(result)

或者,使用选项

shell=True

result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4', shell=True)

(警告:为什么不在 Python 中的 subprocess.Popen 中使用

shell=True

© www.soinside.com 2019 - 2024. All rights reserved.