Search code examples
pythonffmpegyoutubeextractyoutube-dl

YouTube-DL Python details of extracted audio file are not displayed


I have written a small piece of code in python to extract the audio from a YouTube video. Here is the code:

from __future__ import unicode_literals
import youtube_dl

link = input("Enter the video link:")

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    info_dict = ydl.extract_info(link, download=False)
    video_title = info_dict.get('title', None)

path = f'D:\\{video_title}.mp3'

ydl_opts.update({'outtmpl':path})

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([link])

This is the folder where the output audio file is saved: Output File Folder

As you can see, all the details of the audio file are displayed, such as Date Modified, Type and Size.

However, if I change path = f'D:\\{video_title}.mp3' to path = f'D:\\YT_Files\\{video_title}.mp3', then the file details are not getting displayed. Output File Folder

Any idea about why this is so? Is there any way to solve this problem? Any help would be appreciated. Thanks.


Solution

  • Since YT_Files is a directory, you could set the path as path = f'D:\\{video_title}.mp3' which causes metadata to be displayed. Then try to move the file to YT_Files using os.system(). Then you should have metadata on YT_Folders. If not then its fault of Windows explorer i assume. Following code should work but i am not 100% sure this syntax will work on windows.

    from __future__ import unicode_literals
    import youtube_dl
    
    link = input("Enter the video link:")
    
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(link, download=False)
        video_title = info_dict.get('title', None)
    
    path = f'D:\\{video_title}.mp3'
    
    ydl_opts.update({'outtmpl':path})
    
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([link])
    import os
    os.system('move D:\\*.mp3 D:\\YT_Files\\')
    

    Run this code and check if metadata is there or not. Dont check by windows explorer. Go to “Properties”. Click on the “Details” tab and scroll down. If you find the properties with Date-Modified or Type then the problem is in Windows Explorer/File Explorer but if you dont then i will try to debug