我需要从Python中获取音频\视频文件的文件持续时间。
在我的情况下,文件是StringIO对象(字节数组),我无法将此数据保存到文件系统。
我检查了一些库(mutagen,freevo等),但所有这些只适用于文件路径,而不是字节数组。
也许存在可以获取字节数组并给我媒体文件持续时间信息的库?
感谢您的回答!
答案 0 :(得分:0)
尝试discover您的信息
答案 1 :(得分:0)
headers={'Range': 'bytes=0-200','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
def get_video_duration(video_file):
with open(video_file, 'rb') as fp:
data=fp.read()
index=data.find(b'mvhd')+4
time_scale = struct.unpack('>I', data[index + 13:index + 13 + 4])
durations=struct.unpack('>I',data[index + 13 + 4:index + 13 + 4 + 4])
duration = durations[0] / time_scale[0]
return duration
def get_video_duration_url(url):
img_response = requests.get(url=url, headers=headers)
data=img_response.content
index=data.find(b'mvhd')+4
time_scale = struct.unpack('>I', data[index + 13:index + 13 + 4])
durations=struct.unpack('>I',data[index + 13 + 4:index + 13 + 4 + 4])
duration = durations[0] / time_scale[0]
return duration