Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 267
Greg Avatar asked Nov 20 '15 21:11

Greg


2 Answers

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'])
like image 200
jaimeMF Avatar answered Nov 15 '22 03:11

jaimeMF


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!

like image 2
Youssof H. Avatar answered Nov 15 '22 02:11

Youssof H.