Hallo,
es geht um folgende Programm:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | #!/usr/bin/env python3 import csv import re import subprocess import sys from pathlib import Path from pprint import pprint from pymediainfo import MediaInfo def file(file_path): result = subprocess.check_output( ["file", "--brief", str(file_path)], text=True) return [part.strip() for part in result.split(",")] def detect_volumes(file_path): result = subprocess.run( ["ffmpeg", "-hide_banner", "-nostdin", *("-i", str(file_path)), *("-af", "volumedetect"), *("-f", "null"), "/dev/null"], capture_output=True, check=True ) return ( float(re.search(rb"mean_volume: (\S*)", result.stderr)[1]), float(re.search(rb"max_volume: (\S*)", result.stderr)[1]) ) # volumes = { # use dictionary (= HashMap) # match[1]: float(match[2]) # for match in re.finditer(rb"m(ean|ax)_volume: (\S*)", result.stderr) # } # return (volumes[b"ean"], volumes[b"ax"]) def main(): if len(sys.argv) <= 3: print("Usage: ./volume-analysis.py START_PATH .mp3|.flac THRESHOLD [--verbose]") sys.exit(1) start_path = Path(sys.argv[1]) extension = sys.argv[2] threshold = float(sys.argv[3]) verbose = "--verbose" in sys.argv[4:] album_count = 0 album_mean_sum = 0.0 album_mean_min = 0.0 album_mean_max = -128.0 album_peak_sum = 0.0 album_peak_min = 0.0 album_peak_max = -128.0 albums_with_low_volume = [] albums_with_mixed_mode = [] albums_with_mixed_constant_rate = [] files_without_bitrate = [] files_with_comment = [] files_with_notes = [] files_with_audio_comment = [] files_with_errors = [] with ( open(f"mediainfo_albums{extension}.csv", "w", encoding="utf-8", newline="" ) as csv_file, open(f"mediainfo_files{extension}.csv", "w", encoding="utf-8", newline="" ) as verbose_csv_file ) : csv_writer = csv.writer(csv_file, delimiter=';') verbose_csv_writer = csv.writer(verbose_csv_file, delimiter=';') csv_writer.writerow(["No","Path","Size","Mode","Bit rate"]) verbose_csv_writer.writerow(["Album","No","Path","Size","Settings","Channels", "Sampling rate","Bit depth","Mode","Bit rate","Vol mean","Vol max"]) directory_paths = [start_path] + sorted( path for path in start_path.rglob("*") if path.is_dir() ) for directory_path in directory_paths : file_count = 0 mean_sum = 0.0 mean_min = 0.0 mean_max = -128.0 peak_sum = 0.0 peak_min = 0.0 peak_max = -128.0 rate_mode = "None" rate_sum = 0.0 rate = 0.0 rate_old = 0.0 rate_mixed = False # if not (file_paths := sorted( # directory_path.glob("*"+extension, case_sensitive=False) # # directory_path.glob("*.[fF][lL][aA][cC]") # before Python 3.12 # )) : continue if not (file_paths := sorted(path for path in directory_path.glob("*"+extension, case_sensitive=False) # directory_path.glob("*.[fF][lL][aA][cC]") # before Python 3.12 if (extension == ".flac" or not path.with_suffix(".flac").exists()) )) : continue album_count += 1 for file_path in file_paths : if verbose : print(file_path) try: media_info = MediaInfo.parse(file_path) general = media_info.general_tracks[0] audio = media_info.audio_tracks[0] # print(f" general comment: {general.comment}, audio comment: {audio.comment}, abcd: {audio.abcd},") # print(f" audio size: {audio.stream_size} bytes, file size: {general.file_size} bytes.") # pprint(general.to_data()) # pprint(audio.to_data()) if verbose : print(f" Format={audio.format}, Format settings={audio.format_settings},", f"Format profile={audio.format_profile}, Channel(s)={audio.channel_s},", f"Bit rate mode={audio.bit_rate_mode}, Bit rate={audio.bit_rate},", f"Sampling rate={audio.sampling_rate}, Bit depth={audio.bit_depth}") FLAC = "FLAC" in audio.format MP3 = ("MPEG Audio" in audio.format and "Layer 3" in audio.format_profile) or extension == ".mp3" if FLAC or MP3 : mean_volume, max_volume = detect_volumes(file_path) mean_sum += mean_volume mean_min = min(mean_min, mean_volume) mean_max = max(mean_max, mean_volume) peak_sum += max_volume peak_min = min(peak_min, max_volume) peak_max = max(peak_max, max_volume) rate_mode = audio.bit_rate_mode if file_count == 0 else\ rate_mode if audio.bit_rate_mode == rate_mode else "Mixed" if entry := audio.bit_rate : rate = float(entry) rate_mixed = rate_mixed or (file_count > 0 and rate != rate_old) rate_sum += (rate_old := rate) else : files_without_bitrate.append(file_path) if general.comment : files_with_comment.append(file_path) print(f" with general comment: {general.comment}") if general.notes : files_with_notes.append(file_path) print(f" with general notes: {general.notes}") if audio.comment : files_with_audio_comment.append(file_path) print(f" with audio comment: {audio.comment}") file_count += 1 if verbose : tail = " ("+str(audio.bit_rate)+", "+str(audio.bit_rate_mode)+")" print(" mean volume: %5.1f dB, peak volume: %5.1f dB%s" %(mean_volume, max_volume, tail)) # print(f" mean volume: {mean_volume: 5.1f} dB," # f" max volume: {max_volume: 5.1f} dB") # Write values to csv file: values = [album_count, file_count, file_path.name, audio.stream_size, audio.format_settings, audio.channel_s, audio.sampling_rate, audio.bit_depth, audio.bit_rate_mode, audio.bit_rate, mean_volume, max_volume] for value in values : if value == None : value = "" # otherwise crash: "malloc(): invalid size (unsorted)" verbose_csv_writer.writerow(values) else : print (file(file_path)) except subprocess.CalledProcessError as error : files_with_errors.append((file_path, error.returncode)) print(len(file_paths), "files,", len(file_paths) - file_count, "errors in:", directory_path) if extension not in [".flac", ".mp3"] : if verbose : print(); continue if file_count : mean_average = mean_sum / file_count peak_average = peak_sum / file_count rate_average = rate_sum / file_count print("Album volumes: mean: %5.1f | %5.1f | %5.1f dB, peak: %5.1f | %5.1f | %5.1f dB (%1.0f, %s)" %(mean_average, mean_min, mean_max, peak_average, peak_min, peak_max, rate_average, rate_mode)) album_mean_sum += mean_average album_mean_min = min(album_mean_min, mean_min) album_mean_max = max(album_mean_max, mean_max) album_peak_sum += peak_average album_peak_min = min(album_peak_min, peak_min) album_peak_max = max(album_peak_max, peak_max) if peak_max < threshold : albums_with_low_volume.append( (directory_path, mean_sum / file_count, peak_min, peak_max)) if rate_mode == "Mixed" : albums_with_mixed_mode.append((directory_path, rate_average)) if rate_mixed and rate_mode == "CBR" : albums_with_mixed_constant_rate.append((directory_path, rate_average)) if verbose : print() # Write values to csv file: values = [album_count, directory_path, file_count, rate_mode, int(rate_average),] for value in values : if value == None : value = "" # otherwise crash: "malloc(): invalid size (unsorted)" csv_writer.writerow(values) print() print("Directories total: ", len(directory_paths)) print("Albums with "+extension+" files:", album_count) if album_count : album_mean_average = album_mean_sum / album_count album_peak_average = album_peak_sum / album_count print("All albums volumes: mean: %5.1f | %5.1f | %5.1f dB, peak: %5.1f | %5.1f | %5.1f dB" %(album_mean_average, album_mean_min, album_mean_max, album_peak_average, album_peak_min, album_peak_max)) if albums_with_low_volume : print(f"\nAlbums with low volume ({len(albums_with_low_volume)}):") for album, average_mean, peak_min, peak_max in albums_with_low_volume: print("%s\n" " volumes: average mean | min max | max max : %5.1f | %5.1f | %5.1f dB" %(album, average_mean, peak_min, peak_max)) if albums_with_mixed_mode : print(f"\nAlbums with mixed bit rate mode ({len(albums_with_mixed_mode)}):") for album, rate_average in albums_with_mixed_mode: print(f"{album} ==> {rate_average/1000:3.1f} kb/s") if albums_with_mixed_constant_rate : print(f"\nAlbums with mixed constant bit rate ({len(albums_with_mixed_constant_rate)}):") for album, rate_average in albums_with_mixed_constant_rate: print(f"{album} ==> {rate_average/1000:3.1f} kb/s") if files_without_bitrate : print(f"\n{extension} files without bit rate: ({len(files_without_bitrate)}):") for file_path in files_without_bitrate: print(file_path) if files_with_comment : print(f"\n{extension} files with comment: ({len(files_with_comment)}):") for file_path in files_with_comment: print(file_path) if files_with_notes : print(f"\n{extension} files with notes: ({len(files_with_notes)}):") for file_path in files_with_notes: print(file_path) if files_with_audio_comment : print(f"\n{extension} files with audio comment: ({len(files_with_audio_comment)}):") for file_path in files_with_audio_comment: print(file_path) if files_with_errors : print(f"\n{extension} files with errors: ({len(files_with_errors)}):") for file_path, returncode in files_with_errors: print(file_path, "==> return code:", returncode) if __name__ == "__main__" : main() |
Nur in folgendem Ordner taucht dann der Fehler auf (hundert andere Ordner laufen fehlerfrei durch):
$ ./volume-analysis.py "Ablage/Rick Wakeman" .mp3 -1.2 --verbose Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/01 Music Reincarnate, Part I_ The Warning (Introduction).mp3 Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -13.3 dB, peak volume: 0.0 dB (320000, CBR) Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/02 Music Reincarnate, Part I_ The Warning (Conclusion).mp3 Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -13.9 dB, peak volume: 0.0 dB (320000, CBR) Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/03 Music Reincarnate_ The Maker.mp3 malloc(): invalid size (unsorted) Abgebrochen (Speicherabzug geschrieben)
Wenn ich dann in Zeile 106 den Kommentar wegnehme (um zu checken, wie weit das Programm noch kommt), also:
1 | print(f" general comment: {general.comment}, audio comment: {audio.comment}, abcd: {audio.abcd},") |
dann läuft es merkwürdigerweise fehlerfrei durch:
$ ./volume-analysis.py "Ablage/Rick Wakeman" .mp3 -1.2 --verbose Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/01 Music Reincarnate, Part I_ The Warning (Introduction).mp3 general comment: None, audio comment: None, abcd: None, Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -13.3 dB, peak volume: 0.0 dB (320000, CBR) Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/02 Music Reincarnate, Part I_ The Warning (Conclusion).mp3 general comment: None, audio comment: None, abcd: None, Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -13.9 dB, peak volume: 0.0 dB (320000, CBR) Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/03 Music Reincarnate_ The Maker.mp3 general comment: None, audio comment: None, abcd: None, Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -15.6 dB, peak volume: 0.0 dB (320000, CBR) Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/04 Music Reincarnate, Part III_ The Spaceman.mp3 general comment: None, audio comment: None, abcd: None, Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -13.6 dB, peak volume: 0.0 dB (320000, CBR) Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/05 Music Reincarnate_ The Realisation.mp3 general comment: None, audio comment: None, abcd: None, Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -14.9 dB, peak volume: 0.0 dB (320000, CBR) Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/06 Music Reincarnate, Part V_ The Reaper.mp3 general comment: None, audio comment: None, abcd: None, Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -14.6 dB, peak volume: 0.0 dB (320000, CBR) Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/07 The Prisoner.mp3 general comment: None, audio comment: None, abcd: None, Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -13.6 dB, peak volume: 0.0 dB (320000, CBR) Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/08 The Lost Cycle.mp3 general comment: None, audio comment: None, abcd: None, Format=MPEG Audio, Format settings=Joint stereo, Format profile=Layer 3, Channel(s)=2, Bit rate mode=CBR, Bit rate=320000, Sampling rate=44100, Bit depth=None mean volume: -13.8 dB, peak volume: 0.0 dB (320000, CBR) 8 files, 0 errors in: Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection Album volumes: mean: -14.2 | -15.6 | -13.3 dB, peak: 0.0 | 0.0 | 0.0 dB (320000, CBR) Directories total: 5 Albums with .mp3 files: 1 All albums volumes: mean: -14.2 | -15.6 | -13.3 dB, peak: 0.0 | 0.0 | 0.0 dB
Der Crash scheint also etwas mit der print-Funktion zu tun zu haben.
Mit dem mediainfo-Bug hat es diesmal wohl nichts zu tun, denn folgendes läuft fehlerfrei durch:
$ mediainfo "Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/"*.mp3 $ mediainfo "Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection/03 Music Reincarnate_ The Maker.mp3"
Nun habe ich den Ordnerpfad mal eine Ebene tiefer angegeben, also Ablage/Rick Wakeman/Rick Wakeman - No Earthly Connection ... dann läuft das Programm wieder vollständig durch.
Danach habe ich wieder mit dem kürzeren Ordnerpfad probiert, also Ablage/Rick Wakeman ... diesmal läuft das Programm dann merkwürdigerweise doch wieder vollständig durch.
Leite ich die Ausgabe in eine Datei um mit " > Analysis.log" oder " | tee Analysis.log", crashed das Programm wieder.
Der Crash ist also nur bedingt unter gleichen Bedingungen reproduzierbar. Sehr merkwürdig ! Hat jemand eine Idee, wie ich dem Problem auf die Spur kommen kann?
EDIT: Jetzt, 10 Min. später crashed es wieder mit Ablage/Rick Wakeman