pythonmediainfo

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


I have a Python script that I want to use to export specific mediainfo information in multiple subfolders of video and audio files. My problem now is that Python gives me an error message when an audio file in a MOV container does not contain a video stream. How can I tweak the script so that it exports me the audio stream information, but skips the video stream and goes to the next file?

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)) 

The error message looks like this:

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

Solution

  • It is more a Python issue than a MediaInfo one, the question could be more generic.

    I would define the array first, as you do for all_file_data

    [...]
    media_info = MediaInfo.parse(item)
    general_data = []
    video_data = []
    audio_data = []
    for track in media_info.tracks:
    [...]
    

    Note that your code doesn't handle files with more than 1 track of each kind, you may consider to use all_file_data.append() and use e.g. track indexes in your if parts directly instead of using intermediate values. You need to decide how to order information when you have more than 1 track per track kind.