在Python脚本中使用FFProbe [英] Using FFProbe within a Python script

查看:1534
本文介绍了在Python脚本中使用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'不被识别为内部或外部命令,可操作的程序或批处理文件。 

这很令人困惑,我无法在线找到这个确切问题的修复,任何输入都将非常感谢。

解决方案

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 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

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

这篇关于在Python脚本中使用FFProbe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆