how can I set default download location in youtube-dl so that everything that I download with youtube-dl goes into that default directory?
Depending on your needs, I think moving the file afterwards would be just as usefull:
--exec CMD Execute a command on the file after
downloading, similar to find's -exec
syntax. Example: --exec 'adb push {}
/sdcard/Music/ && rm {}'
By creating a function which will move the file
Here is the complete solution I use:
from youtube_dl import YoutubeDL
ydl_opts = {
'format': 'best',
'outtmpl': 'DIR-PATH-HERE%(title)s'+'.mp4',
'noplaylist': True,
'extract-audio': True,
}
video = "https://www.youtube.com/watch?v=SlPhMPnQ58k"
with YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(video, download=True)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
video_length = info_dict.get('duration')
# print(video_title)
This is the EXACT ANOTHER USEFUL method to download your video into a desired DIRECTORY, and also keep the native filename of the download.
This is your config file below:
-o %userprofile%/Desktop/DL/%(title)s-%(id)s.%(ext)s
Options found here Config here
Batch File setup:
::Variables: Set program="%USERPROFILE%\Desktop\YOUTUBE-DL\v20201209\youtube-dl.exe" Set option=best SET MYPATH="%USERPROFILE%\Desktop\YOUTUBE-DL\v20201209\config" SET MYCONFIG="--config-location" SET MYDLDIR="%USERPROFILE%\Desktop\DL" SET INSTR='%%(title)s-%%(id)s.%%(ext)s' MKDIR "%USERPROFILE%\Desktop\DL" ::Ask user for input. Set /P youtubelink=[Past Link]: :: For use of config file, for default download location. %program% -f %option% "%youtubelink%" "%MYCONFIG%" "%MYPATH%" :: There are many ways to accomplish this: :: For Batch File, NOTE extra (%) character needed. :: "%program%" -f "option" --merge-output-format mp4 -o "%MYDLDIR%"\%%(title)s-%%(id)s.%%(ext)s %youtubelink% :: or this use of variable :: "%program%" -f "option" --merge-output-format mp4 -o "%MYDLDIR%"\%INSTR% %youtubelink%
NOTE: The use of "quotes" when there are spaces in your variable options.
Final Message:
Create the config file, put it in a folder (directory) that you wish to refer to it. Go to your youtube-dl.exe file and pass the "parameters" listed above to it using your CMD or a batch file. Done. (contribution, and being kind)
I find a way to directly download files in Downloads folder. I search for long hours. I copied my entire function then you can understand the context around. Here is my code it will maybe helpful for someone:
import os
def download_audio(request):
SAVE_PATH = '/'.join(os.getcwd().split('/')[:3]) + '/Downloads'
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl':SAVE_PATH + '/%(title)s.%(ext)s',
}
link = request.GET.get('video_url')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(["https://www.youtube.com/watch?v="+link])
Tell me if there is a problem.
According to the configuration documentation, you can configure youtube-dl
with a global or user-specific configuration file:
You can configure youtube-dl by placing any supported command line option to a configuration file. On Linux and macOS, the system wide configuration file is located at
/etc/youtube-dl.conf
and the user wide configuration file at~/.config/youtube-dl/config
. On Windows, the user wide configuration file locations are%APPDATA%\youtube-dl\config.txt
orC:\Users\<user name>\youtube-dl.conf
. Note that by default configuration file may not exist so you may need to create it yourself.
On linux, this would be your user config file:
# Save all my videos to the Videos directory:
-o ~/Videos/%(title)s.%(ext)s
-o
switch with the Configuration fileOutput on youtube-dl
is handled with the --output
or -o
switch; pass it as an option, followed by the destination you want to save your downloads to:
youtube-dl -o '%USERPROFILE%\Desktop\%(title)s-%(id)s.%(ext)s' www.youtube.com/link/to/video
Note that -o
has a dual function in that it also sets a template for how your output files will be named, using variables. In this example, it will output the title of the original downloaded video followed by the file extension, which is my personal preference. For all of the variables that can be used in a filename, have a look at the youtube-dl documentation here.
youtube-dl
also allows use of a configuration file - a file that can be used to configure the switches you most frequently use so the program can pull them from there instead, saving you from having to explicitly call them each time you run it. This is what you'll need for the default download location that you're looking for. The configuration file can be used to set a default output destination so that you never have to explicitly set an output again.
To set up a configuration file for youtube-dl, assuming you have Windows:
In %APPDATA%\Roaming
, create a youtube-dl
folder if one doesn't already exist.
Inside that folder, create a plain text file named config.txt
.
Place youtube-dl
options in the file as you'd normally use them on the command line with youtube-dl
, placing each one on a new line. For example, for the output switch, you'd use: -o %USERPROFILE%\Desktop
. For more on the Configuration file, read the documentation on it here.
Even when an option is configured in a configuration file, it can be overridden by calling it explicitly from the command line. So, if you have -o
set in a configuration file to be the default location for downloads, but want to save downloads to somewhere else for a current job, simply calling -o
on the command line will override the configuration file for the current run of the program only.