且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python脚本中使用FFProbe

更新时间:2023-12-05 21:32:58

Python找不到ffprobe,因为它不在您的环境变量中。
单击开始按钮,然后右键单击计算机。从右键单击菜单中选择属性
。在系统窗口中,单击左侧框架中的
高级系统设置链接。





单击系统属性
窗口中的环境变量按钮。它将位于窗口的底部。





在用户变量部分中选择PATH条目。这在环境变量窗口的第一帧中位于
。点击
编辑按钮。在变量值字段中,在
之后输入任何已经写入的值,然后输入; c:\ffmpeg\bin。如果将其复制到不同的
驱动器,请更改驱动器盘符。单击确定保存更改。如果
任何东西在此屏幕中输入不正确,可能导致Windows
无法正常引导。如果用户
变量设置中没有PATH条目,请单击新建按钮并创建一个。为变量名输入PATH
。此方法将为当前的
用户启用FFmpeg。其他Windows用户无法从命令
中运行它。要为所有人启用,请在系统变量中的PATH
条目中输入; c:\ffmpeg\bin。非常小心,不要删除已经在此变量中的任何





打开命令提示符。输入命令ffmpeg -version。如果
命令提示符返回FFmpeg的版本信息,则
安装成功,并且可以从命令提示符中的任何
文件夹访问FFmpeg。



如果您收到libstdc ++ -6是
missing错误,则可能需要安装Microsoft Visual C ++
可再发行组件包


我希望有帮助。



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



我会推荐使用子流程更多的东西(从代码这里):

  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)
#运行ffprobe进程,将stdout解码为utf-8&转换为JSON
ffprobe_output = subprocess.check_output(args).decode('utf-8')

ffprobe_output = json.loads(ffprobe_output)

return ffprobe_output


I am fairly new to python, this is my first real project and I have come to a roadblock. What I have here is a .wmv file, I am using FFprobe to extract the duration in seconds from the .wmv file. When I run the following command in the CMD:

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

I get successful output.

However, when I use os.system, like this:

os.system('ffprobe -i Video2.wmv -show_entries format=duration -v quiet -of csv="p=0"')

I get the following output:

'ffprobe' is not recognized as an internal or external command, operable program or batch file.

This is very confusing and I haven't been able to find a fix to this exact problem online, any input would be very much appreciated.

Python can't find ffprobe because it isn't in your environment variables. This *** video shows how to properly install it, as does this wikihow page (method 2), which I'll quote from here:

Enabling FFmpeg in the Command Line

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.

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

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.

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.

I hope that helps.

Just a side note, I don't think that os.system is the recommended way of calling the command line like that any more.

I'd recommend something more like this using subprocess instead (adapted from code 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