'Pydub's mediainfo return an empty list on a file that has metadata. How can I fix this?
I've verified that the files I'm working with all have metadata and I want to transfer them to another file. I'm not sure what's going on here. Is it a problem with the library, ffmpeg? The lines tags=mediainfo(wav_file).get('TAG', {}) and tags=mediainfo(flac_file).get('TAG', {}) were taken directly from this answer on Pydub's Github issues. I added print(mediainfo(...)) just to show that the files are returning an empty dictionary of {} for me.
import os
import pydub
from pydub.utils import which, mediainfo
from tkinter.filedialog import askopenfilename,askopenfilenames
from mutagen.flac import FLAC
from mutagen.mp3 import EasyMP3
from mutagen.id3 import ID3, APIC
pydub.AudioSegment.converter = which("ffmpeg")
#pydub.AudioSegment.ffmpeg = r"C:\ffmpeg\ffmpeg\bin\ffmpeg.exe"
#pydub.AudioSegment.avprobe =r"C:\Program Files\libav-i686-w64-mingw32-20160101\usr\bin\avprobe.exe"
def select_files(filetype):
if filetype == 'wav':
filetypes = (
('WAV files', '*.wav'),
('All files', '*.*')
)
filename = askopenfilenames(
title='Open WAV files',
initialdir='/',
filetypes=filetypes
)
return filename
elif filetype == 'flac':
filetypes = (
('FLAC files', '*.flac'),
('All files', '*.*')
)
filename = askopenfilenames(
title='Open FLAC files',
initialdir='/',
filetypes=filetypes
)
return filename
else:
print("Not a valid file type.")
print("Converting WAV or FLAC? ")
file_type = input("").lower()
if file_type.lower() == 'wav':
wav_files = select_files('wav')
for wav_file in wav_files:
print(mediainfo(wav_file))
mp3_file = os.path.splitext(wav_file)[0] + '.mp3'
sound = pydub.AudioSegment.from_wav(wav_file).export(mp3_file, format="mp3", bitrate="320k", tags=mediainfo(wav_file).get('TAG', {}))
print("Conversion complete.")
elif file_type.lower() == 'flac':
flac_files = select_files('flac')
for flac_file in flac_files:
print(mediainfo(flac_file))
mp3_file = os.path.splitext(flac_file)[0] + '.mp3'
sound = pydub.AudioSegment.from_file(flac_file).export(mp3_file, format="mp3", bitrate="320k", tags=mediainfo(flac_file).get('TAG', {}))
print("Conversion complete.")
else:
print("Not a valid file type.")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
