youtube-dl: setting metadata attributes and embedding thumbnail from python?
Asked Answered
S

2

5

How do you set meta attributes and embed thumbnail from the actual python code? I can easily do the embedding and adding meta atributes from the command line with something like:

youtube-dl https://www.youtube.com/watch?v=5wK5-ChsDsQ -x --audio-format mp3 --add-metadata --xattrs --embed-thumbnail --prefer-ffmpeg --postprocessor-args "-metadata comment='my comment'" -o 'yt_%(id)s_.mp3' --verbose

The documentation for the python code shows a basic example but nothing advanced such as adding metadata and embedding thumbnails.

Stentor answered 20/11, 2015 at 21:45 Comment(3)
(Why on earth would you use sudo to download a file?)Ribband
What documentation? Can you please post the link?Erhart
link to documentationStentor
G
8

You have to add the postprocessors:

from __future__ import unicode_literals
import youtube_dl

ydl_opts = {
    'writethumbnail': True,
    'postprocessors': [
        {
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
        },
        {'key': 'EmbedThumbnail'},
        {'key': 'FFmpegMetadata'},
    ],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=5wK5-ChsDsQ'])
Gratitude answered 5/2, 2016 at 16:11 Comment(0)
E
2

I prefer commands over python since it has well-explained documentation. So I suggest you make a bash script that contains your preferred command. Then run the python program that will call the bash-script and runs your commands.

Linux: yt_script.sh:

#!/bin/sh
youtube-dl $1 -x --audio-format mp3 --add-metadata --xattrs --embed-thumbnail --prefer-ffmpeg --postprocessor-args "-metadata comment='my comment'" -o 'yt_%(id)s_.mp3' --verbose
exit 0

and in the python file you call the bash script giving the url as a parameter.

python_program.py:

import subprocess
subprocess.call(["Path/to/yt_script.sh","https://www.youtube.com/watch?v=5wK5-ChsDsQ"])

Don't forget to give your script the correct permissions by typing chmod u+x Path/to/yt_script.sh in the terminal.

Then run python /Path/to/python_program.py to run the program from terminal.

You may also need to pass arguments (youtube URLs) to the command. You can do that by editing "python_program.py" as following:

import subprocess
import sys
subprocess.call(["Path/to/yt_script.sh",sys.argv[1]])

Then all you have to do is to open the terminal, run the python program, add the YouTube URL at the end of the command as follows:python /Path/to/python_program.py https://www.youtube.com/watch?v=z60S2DCqDpY

I hope this clears things out!

Envision answered 4/11, 2019 at 17:46 Comment(1)
Awesome example for both youtube-dl and shell script execution in python :)Denitrify

© 2022 - 2024 — McMap. All rights reserved.