使用ffmpeg获取python中的视频持续时间 [英] Using ffmpeg to obtain video durations in python

查看:856
本文介绍了使用ffmpeg获取python中的视频持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的电脑上安装了ffprobe命令ffprobe,并在这里



但是,我仍然无法运行列出的代码这里



我尝试使用以下代码失败。


语法错误:第12行文件GetVideoDurations.py
中的非ASCII字符'\xe2',但未声明编码;见
http://python.org/dev/peps/pep-0263/ 的详细信息


有谁知道有什么问题?我没有正确地引用目录?我需要确保.py和视频文件位于特定位置吗?

  import subprocess 

def getLength(filename):
result = subprocess.Popen [ffprobe,filename],
stdout = subprocess.PIPE,stderr = subprocess.STDOUT)
return [x for result.stdout.readlines()ifDurationin x]

fileToWorkWith ='C:\Users\PC\Desktop\Video.mkv'

getLength(fileToWorkWith)

如果问题有些基础,抱歉所有我需要的是能够遍历一组视频文件,并获得他们的开始时间和结束时间。



谢谢!

解决方案

不需要迭代 FFprobe 的输出。有一个简单的命令只会导致输入文件的持续时间。

  ffprobe -i input_audio -show_entries format = duration -v quiet -of csv =p = 0

您可以使用以下方法来获取持续时间。

  def getLength(input_video):
result = subprocess.Popen('ffprobe -i input_video -show_entries format =持续时间-v quiet -of csv =p = 0',stdout = subprocess.PIPE,stderr = subprocess.STDOUT)
output = result.communicate()
return output [0]

此外,您还需要为每个文件提供绝对路径。



希望这有帮助!


I've installed ffprobe using the pip ffprobe command on my PC, and installed ffmpeg from here.

However, I'm still having trouble running the code listed here.

I try to use the following code unsuccessfully.

SyntaxError: Non-ASCII character '\xe2' in file GetVideoDurations.py on line 12, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Does anyone know what's wrong? Am I not referencing the directories correctly? Do I need to make sure the .py and video files are in a specific location?

import subprocess

def getLength(filename):
  result = subprocess.Popen(["ffprobe", "filename"],
    stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
  return [x for x in result.stdout.readlines() if "Duration" in x]

fileToWorkWith = ‪'C:\Users\PC\Desktop\Video.mkv'

getLength(fileToWorkWith)

Apologies if the question is somewhat basic. All I need is to be able to iterate over a group of video files and get their start time and end time.

Thank you!

解决方案

There is no need to iterate though the output of FFprobe. There is one simple command which result only the duration of the input file.

ffprobe -i input_audio -show_entries format=duration -v quiet -of csv="p=0"

you can use the following method instead to get the duration.

def getLength(input_video):
    result = subprocess.Popen('ffprobe -i input_video -show_entries format=duration -v quiet -of csv="p=0"', stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    output = result.communicate()
    return output[0]

Also you need to provide the absolute path to each of your file.

Hope this helps!

这篇关于使用ffmpeg获取python中的视频持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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