CMD; youtube-dl: store the output filename as a variable

Solution 1:

The following command downloads a YouTube video and names it with the same title that it has on YouTube, followed by the downloaded video's extension.

youtube-dl -o "%(title)s.%(ext)s" URL

The following command downloads only the video's title and extension and displays the result in the next line after the command.

youtube-dl --get-filename -o "%(title)s.%(ext)s" URL

The following command in a Windows batch file downloads only the video's title and extension and stores the result in a variable called my_variable.

for /f "delims=" %%a in ('youtube-dl --get-filename -o "%(title)s.%(ext)s" URL') do @set my_variable=%%a

In all three commands you may also use multiple URLs separated by space characters instead of a single URL. You may also use the --batch-file FILE option to replace the URL(s) with a list of URLs stored in a batch file (e.g. FILE).

If you can't download a YouTube video with youtube-dl it's usually because youtube-dl isn't updated to the latest version. Upgrade youtube-dl to the latest version in Windows with pip:

pip install --upgrade youtube-dl

In macOS upgrade youtube-dl to the latest version with homebrew. Many Linux distributions support snap packages. This allows the user to install the youtube-dl snap package which is automatically updated. To install the youtube-dl snap package run the following command:

sudo snap install youtube-dl # to launch youtube-dl run snap run youtube-dl