0

I need to get file duration from audio\video files in Python.

In my case file is StringIO object (byte array) and I have no way to save this data to file system.

I checked some libraries (mutagen, freevo, etc.) but all of this working only with file path, not with byte array.

Maybe exist library that can get byte array and give me media file duration information?

Thanks for your answers!

4

2 回答 2

0

尝试发现您的信息

于 2013-08-11T03:35:56.037 回答
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
于 2021-01-18T11:48:49.840 回答