在Linux上,YouTube将临时Flash文件放在/ tmp中。 Nautilus可以显示它们的持续时间(分钟:秒),但我还没有找到使用python提取持续时间的方法。 您的方法所需的依赖项越少越好。
提前致谢。
答案 0 :(得分:3)
使用ffmpeg可以完成的一种方法。 ffmpeg需要安装h.264和h.263编解码器支持。接下来是检索视频持续时间的命令,可以通过python system(command)
调用
ffmpeg -i flv_file 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//
答案 1 :(得分:1)
虽然这个例子可能看起来过于复杂,但我把它作为一个练习来更好地理解Python,它使得处理文件持续时间的原子部分变得更容易。
#!/usr/bin/env python
"""
duration
=========
Display the duration of a video file. Utilizes ffmpeg to retrieve that information
Usage:
#duration file
bash-3.2$ ./dimensions.py src_video_file
"""
import subprocess
import sys
import re
FFMPEG = '/usr/local/bin/ffmpeg'
# -------------------------------------------------------
# Get the duration from our input string and return a
# dictionary of hours, minutes, seconds
# -------------------------------------------------------
def searchForDuration (ffmpeg_output):
pattern = re.compile(r'Duration: ([\w.-]+):([\w.-]+):([\w.-]+),')
match = pattern.search(ffmpeg_output)
if match:
hours = match.group(1)
minutes = match.group(2)
seconds = match.group(3)
else:
hours = minutes = seconds = 0
# return a dictionary containing our duration values
return {'hours':hours, 'minutes':minutes, 'seconds':seconds}
# -----------------------------------------------------------
# Get the dimensions from the specified file using ffmpeg
# -----------------------------------------------------------
def getFFMPEGInfo (src_file):
p = subprocess.Popen(['ffmpeg', '-i', src_file],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
return stderr
# -----------------------------------------------------------
# Get the duration by pulling out the FFMPEG info and then
# searching for the line that contains our duration values
# -----------------------------------------------------------
def getVideoDuration (src_file):
ffmpeg_output = getFFMPEGInfo (src_file)
return searchForDuration (ffmpeg_output)
if __name__ == "__main__":
try:
if 2==len(sys.argv):
file_name = sys.argv[1]
d = getVideoDuration (file_name)
print d['hours'] + ':' + d['minutes'] + ':' + d['seconds']
else:
print 'Error: wrong number of arguments'
except Exception, e:
print e
raise SystemExit(1)