Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python从字节数组获取媒体文件元数据_Python_Metadata_Media_Mutagen - Fatal编程技术网

Python从字节数组获取媒体文件元数据

Python从字节数组获取媒体文件元数据,python,metadata,media,mutagen,Python,Metadata,Media,Mutagen,我需要从Python中的音频/视频文件中获取文件持续时间 在我的例子中,文件是StringIO对象(字节数组),我无法将这些数据保存到文件系统 我检查了一些库(诱变剂、freevo等),但所有这些都只使用文件路径,而不使用字节数组 可能存在可以获取字节数组并向我提供媒体文件持续时间信息的库 谢谢你的回答 尝试从 headers={'Range': 'bytes=0-200','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Appl

我需要从Python中的音频/视频文件中获取文件持续时间

在我的例子中,文件是StringIO对象(字节数组),我无法将这些数据保存到文件系统

我检查了一些库(诱变剂、freevo等),但所有这些都只使用文件路径,而不使用字节数组

可能存在可以获取字节数组并向我提供媒体文件持续时间信息的库

谢谢你的回答

尝试从
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