Search code examples
pythonaudiomp3youtube-dl

Audio downloaded from youtube_dl python module is encoded in AAC. How to fix it?


So I wanna download some music for our car stereo. Wanted to use an online mp3 converter but the ads are too much. So I made a little piece of code:

import youtube_dl
def mp3_convert():
    print('processing...')
    link = ['https://youtu.be/LaH9b6Lqwzo']
    info = youtube_dl.YoutubeDL().extract_info(url=link, download=False)
    file_name = '{}.mp3'.format(info['title'])
    options = {
            'format' : 'bestaudio/best',
            'keepvideo' : False,
            'outtmpl' : file_name
            }

    with youtube_dl.YoutubeDL(options) as ydl:
        ydl.download([info['webpage_url']])
        print('completed!')

mp3_convert()

Well tbh, it works on pc. It play with Windows' Groove. But it won't play on audacity and the car's stereo. Later on I found this thread which lead me to believe that the problem is because of the AAC encoding as it assumes that I was gonna convert it to mp4(?). Now, is there any chance I could fix it? I mean, I could download it all over again as long as it works.


Solution

  • Ok so I finally found a workaround... Well, it's pretty much the solution. So basically, what I downloaded were mp4 files that are audio-only with a ".mp3" extension. What I described might sound like an mp3 but it isn't.

    So what I did was use ffmpeg to convert the mp4 file to mp3. I added something to my code where after it downloads the file, it converts it from mp4 to mp3 with ffmpeg using the subprocess module of python. subprocess.run(['ffmpeg', '-i', file.mp4, 'file.mp3'])