Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add album art, track and artist data to file with YouTube-DL Embedded in Python #12225

Closed
ghost opened this issue Feb 23, 2017 · 3 comments
Closed

Comments

@ghost
Copy link

@ghost ghost commented Feb 23, 2017

How would i use the ytd_opts{} to add track data to the converted mp3 file, specifcally the album art, artist, and album?

@dstftw
Copy link
Collaborator

@dstftw dstftw commented Feb 23, 2017

Add FFmpegMetadata postprocessor to postprocessors list.

@dstftw dstftw closed this Feb 23, 2017
@ghost
Copy link
Author

@ghost ghost commented Feb 25, 2017

How would I include "artist", "description" etc?

@marcwebbie
Copy link
Contributor

@marcwebbie marcwebbie commented Mar 12, 2017

@dyablade looks lke you need a custom PostProcessor for it since FFmpegMetadata only parses the info already generated:

import youtube_dl
from youtube_dl.postprocessor.ffmpeg import FFmpegMetadataPP


class MyLogger(object):
    def debug(self, msg):
        pass

    def warning(self, msg):
        pass

    def error(self, msg):
        print(msg)


def console_hook(d):
    if d['status'] == 'finished':
        print('Done downloading, now converting ...')

    if d['status'] == 'downloading':
        if d.get('eta') is not None:
            print(d['_percent_str'])
        else:
            print('Unknown ETA')


class FFmpegMP3MetadataPP(FFmpegMetadataPP):

    def __init__(self, downloader=None, metadata=None):
        self.metadata = metadata or {}
        super(FFmpegMP3MetadataPP, self).__init__(downloader)

    def run(self, information):
        information = self.purge_metadata(information)
        information.update(self.metadata)
        return super(FFmpegMP3MetadataPP, self).run(information)

    def purge_metadata(self, info):
        info.pop('title', None)
        info.pop('track', None)
        info.pop('upload_date', None)
        info.pop('description', None)
        info.pop('webpage_url', None)
        info.pop('track_number', None)
        info.pop('artist', None)
        info.pop('creator', None)
        info.pop('uploader', None)
        info.pop('uploader_id', None)
        info.pop('genre', None)
        info.pop('album', None)
        info.pop('album_artist', None)
        info.pop('disc_number', None)
        return info


url = 'https://www.youtube.com/watch?v=EL8e2ujXe8g'
metadata = {
    "title": "Stop",
    "artist": "Movie",
}
ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
    'outtmpl': 'result.%(format)s',
    'logger': MyLogger(),
    'progress_hooks': [console_hook],
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ffmpeg_mp3_metadata_pp = FFmpegMP3MetadataPP(ydl, metadata)
    ydl.add_post_processor(ffmpeg_mp3_metadata_pp)
    ydl.download([url])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.