Reputation: 4235
YouTube-DL
saves by default in the working directory, unless you specifically direct it otherwise in the youtube-dl.conf
file. However, this file doesn't exist on install, and I'm having a lot of trouble creating it.
I'm running an Ubuntu 16.04 LAMP stack server on Digital Ocean.
I tried creating youtube-dl.conf
under /usr/local/etc
and then added this to it: --o ~/html/media/audio/%(title)s
but that does nothing.
I tried the solution in this answer:
mkdir -p ~/.config/youtube-dl/
echo "-o ~/html/media/audio/%(title)s" > ~/.config/youtube-dl/config
And it worked without any issues, but I can't find where it made the directory, and either way it doesn't work.
I can't simply do something like youtube-dl -o "~/Desktop/%(title)s.%(ext)s" 'youtube file url'
because I'm using youtube-dl
from a Python script, and not from command line. Running the above command without a URL doesn't work. Related askubuntu question.
Can anyone help me out? The working directory is /var/www/html
, but I need it to save in /var/www/html/media/audio
. Still new to Ubuntu. Thank you!
Youtube-DL Github Configuration Section
Edit: Using the answer by @phihag below, I added this:
subprocess.check_call(['youtube-dl', '--output', '/var/www/html/media/audio/%(title)s.%(ext)s', url])
But this is saving the file in mkv
and I need it in mp3
format. I've tried something like:
subprocess.check_call(['youtube-dl', '--audio-format', 'mp3', '--output', '/var/www/html/media/audio/%(title)s.%(ext)s', url])
And other variations, but I get something like this error:
subprocess.CalledProcessError: Command '['youtube-dl', '--extract-audio --audio-format mp3 --output '/var/www/html/media/audio/%(title)s', 'url']' returned non-zero exit status 2
These are the options I was previously using that I'm now trying to emulate using the subprocess
:
ydl_opts = {
'fixup': 'detect_or_warn',
'format': 'bestaudio/best',
'extractaudio': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
Using these options above does not allow for changing the default location. That has to be done on a case-by-case basis through command line, each time you want to download, or you need to use the config file (to my understanding. I haven't seen any examples in my searching where someone can specify the default save location through the API in a script).
Upvotes: 0
Views: 2374
Reputation:
There is no need to write a configuration file; you can set the output template from python as well:
from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'outtmpl': '/var/www/html/media/audio/%(title)s.%(ext)s',
'extractaudio': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])
Upvotes: 2