0

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
1
  • Please edit your question to give an example of the command you run to download and the corresponding output. What do you want to do with the variables? – DavidPostill Dec 16 '15 at 16:57
7

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

3
  • 1
    I edited the question - I need to use this variable later in CMD batch. – Rayearth Dec 17 '15 at 18:30
  • Since you need to use this variable later in a CMD batch, you may have to write a loop in order to copy the data that is stored in $my_variable to somewhere else in case you want to reuse $my_variable for storing new data. – karel Dec 17 '15 at 18:41
  • how would that $my_var working(if it works)? given that normally from cmd you do set a=6, you can't do a=6 or $a=6 from cmd.exe i.imgur.com/i6B5LyZ.png If that's different with youtube-dl then why is that? I haven't tested what you wrote, (though I would if you had a 1MB little youtube video conveniently mentioned in your answer to test it with). – barlop Dec 18 '15 at 0:51
0

I can't comment 'karel' now, so for Windows with youtube-dl.exe in some folder:

for /f "delims=" %%a in ('%~d0"%~p0"youtube-dl.exe --get-filename -o "%%(title)s.%%(ext)s" https://www.youtube.com/watch^?v^=O1abcdefgh') do set my_variable=%%a

Windows. Download video and audio in the best quality to your desktop with the original name with the url taken from the clipboard.

setlocal enabledelayedexpansion
for %%I in (powershell.exe) do if "%%~$PATH:I" neq "" (
    set getclip=powershell "Add-Type -AssemblyName System.Windows.Forms;$tb=New-Object System.Windows.Forms.TextBox;$tb.Multiline=$true;$tb.Paste();$tb.Text"
)
for /f "delims=" %%I in ('%getclip% ^| findstr /n "^"') do (
    set "line=%%I" & set "line=!line:*:=!"
)
set "psCommand="[Environment]::GetFolderPath('DesktopDirectory')""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "Desktop=%%I"
%~d0"%~p0"youtube-dl.exe -f "bestvideo[height<=1080]+bestaudio/best[height<=1080]" -o "!Desktop!\%%(title)s.%%(ext)s" !line!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.