0

I'm trying to download a song from YouTube and write the artist's name to the metadata. embed-metadata takes care of this most of the time, but in some cases, the uploader isn't the same as the artist.

import yt_dlp


def download(song, url):
    ydl_opts = {
        'format': 'm4a/bestaudio/best',
        'verbose': True,
        'outtmpl': (song + ".%(ext)s"),
        'merge-output-format': 'mkv',
        'embed-metadata': True,
        'parse-metadata': '%(artist)s - %(title)s',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'aac'
        },
            {
                'key': 'FFmpegMetadata'
            }
        ]
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download(url)


download('Daniel Pemberton - Falling Apart',
         'https://www.youtube.com/watch?v=Uhx77_beiso&pp=ygUNZmFsbGluZyBhcGFydA%3D%3D')

This specific song's uploader on YouTube is SonySoundtracksVevo, but I want to change it to Daniel Pemberton, the name of the artist. Is there anyway to do this with yt-dlp? I've already tried 'parse-metadata': '%(artist)s - %(title)s' but the artist name is still incorrect.

2 Answers2

0

The reason for the error is that you are using the parse-metadata settings incorrectly.

% sign is not recognized by yt_dlp so it doesn't work. Instead, you can get the metadata information using the metadata_from_field settings in the extractor_arhs section.

{'metadata_from_field': 'artist'} 

also here's the docmunet for yt_dlp https://github.com/yt-dlp/yt-dlp

In here you can check Modifying metadata examples.

0

I solved it by adding the MetadataParserPP and EmbedThumbnail postprocessors.

import yt_dlp
from yt_dlp.postprocessor import MetadataParserPP


def download(song, url):
    ydl_opts = {
        'format': 'm4a/bestaudio/best',
        'outtmpl': song + ".%(ext)s"),
        'merge-output-format': 'mkv',
        'writethumbnail': True,
        'embedthumbnail': True,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'aac'
        },
        {
            'key': 'MetadataParser',
            'when': 'post_process',
            # The title and artist name are interpreted from the given song name, 
            # but can be interpreted from the title on YouTube by replacing song with "title"
            'actions': [(MetadataParserPP.Actions.INTERPRET, song, '(?P<artist>.+)\ \-\ (?P<title>.+)')]
        },
        {
            'key': 'FFmpegMetadata'
        },
        {
            "key": 'EmbedThumbnail'
        }
        ]
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download(url)


download('Daniel Pemberton - Falling Apart', 'https://www.youtube.com/watch?v=Uhx77_beiso&pp=ygUNZmFsbGluZyBhcGFydA%3D%3D')