python - 在 Python 脚本中使用 FFProbe

标签 python ffmpeg ffprobe

我对python相当陌生,这是我的第一个真正的项目,我遇到了障碍。我这里有一个 .wmv 文件,我正在使用 FFprobe 从 .wmv 文件中提取持续时间(以秒为单位)。当我在 CMD 中运行以下命令时:

ffprobe -i Video2.wmv -show_entries format=duration -v quiet -of csv="p=0"

我得到成功的输出。

但是,当我使用 os.system 时,像这样:
os.system('ffprobe -i Video2.wmv -show_entries format=duration -v quiet -of csv="p=0"')

我得到以下输出:
'ffprobe' is not recognized as an internal or external command, operable program or batch file.

这非常令人困惑,我无法在网上找到解决这个确切问题的方法,非常感谢任何输入。

最佳答案

Python 找不到 ffprobe 因为它不在您的环境变量中。 This youtube video显示如何正确安装它,this wikihow page (方法2),我将从这里引用:

Enabling FFmpeg in the Command Line

enter image description here Click the Start button and right-click on Computer. Select Properties from the right-click menu. In the System window, click on the “Advanced system settings” link in the left frame.

enter image description here

Click the Environmental Variables button in the System Properties window. It will be located at the bottom of the window.

enter image description here

Select the PATH entry in the "User variables" section. This is located in the first frame in the Environmental Variables window. Click the Edit button. In the “Variable value” field, enter ;c:\ffmpeg\bin after anything that's already written there. If you copied it to a different drive, change the drive letter. Click OK to save your changes. If anything is entered incorrectly in this screen, it could cause Windows to be unable to boot properly. If there is no PATH entry in the "User variables" setting, click the New button and create one. Enter PATH for the variable name. This method will enable FFmpeg for the current user. Other Windows users will not be able to run it from the command line. To enable it for everyone, enter ;c:\ffmpeg\bin in the PATH entry in "System variables". Be very careful not to delete anything that is already in this variable.

enter image description here

Open the command prompt. Enter the command “ffmpeg –version”. If the command prompt returns the version information for FFmpeg, then the installation was successful, and FFmpeg can be accessed from any folder in the command prompt.

If you receive a “libstdc++ -6 is missing” error, you may need to install the Microsoft Visual C++ Redistributable Package, which is available for free from Microsoft.



我希望这会有所帮助。

只是一个旁注,我不认为 os.system是推荐的调用命令行的方式。

我建议使用 subprocess 来代替(改编自代码 here ):
import subprocess
import shlex
import json
def get_duration(file_path_with_file_name):

    cmd = 'ffprobe -show_entries format=duration -v quiet -of csv="p=0"'
    args = shlex.split(cmd)
    args.append(file_path_with_file_name)
    # run the ffprobe process, decode stdout into utf-8 & convert to JSON
    ffprobe_output = subprocess.check_output(args).decode('utf-8')

    ffprobe_output = json.loads(ffprobe_output)

    return ffprobe_output

关于python - 在 Python 脚本中使用 FFProbe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41728249/

相关文章:

python - 是否可以获取查询开头的游标,以便查询的 future 运行可以将其用作 end_cursor?

python - 如何将多个 NumPy 数组输入 Keras 中的深度学习网络?

python - 使用moviepy包(Python)的问题

c - 使用 libavcodec 保存 H.264 编码图像

c - FFmpeg AVFrame 音频数据修改

python - 使用 Python 中的 Selenium 禁用 Chrome 中的图像加载

Python 'str.contains' 函数未返回正确的值

python - 从 ffmpeg 字符串结果创建关联数据到 python

ffmpeg - mediainfo 和 ffprobe 返回的 channel 数差异

ffprobe - 如何限制ffprobe解释的帧数?