Using FFProbe within a Python script

2019-09-15 17:40发布

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.

1条回答
神经病院院长
2楼-- · 2019-09-15 18:01

Python can't find ffprobe because it isn't in your environment variables. This youtube 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

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.

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
查看更多
登录 后发表回答