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

cmd.exeyoutube-dl

How to store the output filename and extension as a variable after downloading the movie using youtube-dl and use it later in CMD batch? The following is an example of a command to download a video from YouTube to the current directory where URL is the URL of the video.

youtube-dl URL

Store the output filename in a variable called my_variable.

Edit:
To be completely specific I want to download a video with youtube-dl using a simple cmd batch file that will provide a CHOICE command at the end with option to open the output file with MPC-HC.

I need the method of putting into a variable the output of

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

so I can use it later for the following line

"C:\Program Files\MPC-HC\mpc-hc64.exe" "D:\Downloads\%my_variable%" /play

Best Answer

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).

Related Question