如何用pymediainfo导出多个Mediainfo导出的影音文件?

How to export multiple Mediainfo exports of video and audio files with pymediainfo?

我有一个 Python 脚本,我想用它导出视频和音频文件的多个子文件夹中的特定媒体信息信息。我现在的问题是,当 MOV 容器中的音频文件不包含视频流时,Python 给我一条错误消息。我如何调整脚本,以便它向我导出音频流信息,但跳过视频流并转到下一个文件?

import os
import glob
from pymediainfo import MediaInfo
import csv
import argparse

parser = argparse.ArgumentParser()
parser.description = "Survey a directory for AV files and report on technical metadata, Video Codec and Audio Codec"
parser.add_argument("-d", "--directory",
                    required = True,
                    help = "Path to a directory of AV files")
parser.add_argument("-e", "--extension",
                    required = True,
                    help = "Extension of AV file to survey")
parser.add_argument("-o", "--output",
                    required = True,
                    help = "Path to the save the metadata as a CSV")
args = parser.parse_args()

print(args.directory, args.extension, args.output)


file_dir = args.directory
file_list = glob.glob(os.path.join(file_dir, '**', '*' + args.extension), recursive=True)                     

all_file_data = []

for item in file_list: 
    #size_list.append(os.stat(item).st_size) 
    media_info = MediaInfo.parse(item)
    for track in media_info.tracks:
        if track.track_type == 'General':
                general_data = [
                    track.complete_name, 
                    track.file_extension]
        elif track.track_type == 'Video':
                video_data = [
                    track.format,
                    track.codec_id,
                    track.width,
                    track.height,
                    track.display_aspect_ratio]
        elif track.track_type == 'Audio':
                audio_data = [
                    track.format,
                    track.codec_id]
    all_file_data.append(general_data + video_data + audio_data)

    
with open(args.output, 'w') as f: 
    md_csv = csv.writer(f)
    md_csv.writerow([
        'Complete Name',
        'Extension',
        'Video Format',
        'Video Codec',
        'Width',
        'Height',
        'Display aspect ratio',
        'Audio Format',
        'Audio Codec'
    ])
    md_csv.writerows(sorted(all_file_data)) 

错误消息如下所示:

ralphmichel@pop-os:~/Schreibtisch/script_test$ python3 av-survey.py -d '/home/ralphmichel/Dokumente/LTLYM_website/assets/images' -e mov -o out.csv
/home/ralphmichel/Dokumente/LTLYM_website/assets/images mov out.csv
Traceback (most recent call last):
  File "/home/ralphmichel/Schreibtisch/script_test/av-survey.py", line 68, in <module>
    all_file_data.append(general_data + video_data + audio_data)
NameError: name 'video_data' is not defined

与其说是 MediaInfo 问题,不如说是 Python 问题,问题可能更笼统。

我会先定义数组,就像您对 all_file_data

所做的那样
[...]
media_info = MediaInfo.parse(item)
general_data = []
video_data = []
audio_data = []
for track in media_info.tracks:
[...]

请注意,您的代码无法处理每种类型的曲目超过 1 条的文件,您可以考虑使用 all_file_data.append() 并使用例如直接跟踪 if 部分中的索引,而不是使用中间值。当每个曲目种类超过 1 个曲目时,您需要决定如何订购信息。