0
votes

I'm trying to

  1. Using a single batch script to
  2. Download an mp4 video using youtube-dl
  3. Save the video's original title to a batch variable
  4. Convert the video to webm with FFmpeg

I want to keep the original title, so it needs to read the title with youtube-dl, save it to a variable, and use that variable for FFmpeg input/output filename.


CMD Batch

1. Download Video

youtube-dl -f best "https://www.youtube.com/watch?v=TWNhqCHw0qc" -o "C:\Users\Matt\Downloads\%%(title)s.mp4" --merge-output-format mp4

2. Download Video using Loop

This is used to save the title to a variable %%a.

for /f "delims=" %%a in ('youtube-dl -f best "https://www.youtube.com/watch?v=TWNhqCHw0qc" -o @"C:\Users\Matt\Downloads\%%(title)s.mp4" --merge-output-format mp4') do (echo example)

3. Final Script
Download Video, Save Title, Convert with FFmpeg

Sorted

for /f "delims=" %%a in ('
    youtube-dl 
    -f best "https://www.youtube.com/watch?v=TWNhqCHw0qc" 
    -o @"C:\Users\Matt\Downloads\%%(title)s.mp4" 
    --merge-output-format mp4
    ')

do (ffmpeg -y 
    -i "C:\Users\Matt\Downloads\%%a.mp4" 
    -c:v libvpx -b:v 1300K -crf 16 -pix_fmt yuv420p 
    -map 0:v:0? -sn 
    -c:a libvorbis -q:a 6 -ac 2 -map 0:a:0? 
    -f webm
    "C:\Users\Matt\Downloads\%%a.webm"
    )

Inline

for /f "delims=" %%a in ('youtube-dl -f best "https://www.youtube.com/watch?v=TWNhqCHw0qc" -o @"C:\Users\Matt\Downloads\%%(title)s.mp4" --merge-output-format mp4') do (ffmpeg -y -i "C:\Users\Matt\Downloads\%%a.mp4" -c:v libvpx -b:v 1300K -crf 16 -pix_fmt yuv420p -map 0:v:0? -sn -c:a libvorbis -q:a 6 -ac 2 -map 0:a:0? -f webm "C:\Users\Matt\Downloads\%%a.webm")

Error

Before the script can ever reach FFmpeg, youtube-dl fails to download the file. It says the file has already been downloaded, even when there is no file in the directory.

[download] @C#\Users\Matt\Downloads\Color Balloons.mp4 has already been downloaded

1
In a batch file, FOR variables must use two % symbols. Says so on the eighth line of the help file. Also since the % symbol is a special character, if you need to use a literal % you must also double it to two % symbols. - Squashman
@Squashman That's when using it in a file, when copy pasting into cmd it can use one %. - Matt McManis
I'm sending this script over to cmd from another C# program, if I use two % it fails. - Matt McManis
Yes. I never said you couldn't do that. That is why I quoted the help file. But you literally wrote BATCH how many times in your question and used the BATCH-FILE tag. - Squashman
Then remove the word batch from your question and remove the BATCH-FILE tag. - Squashman

1 Answers

0
votes

I was able to create a script that works.

One of the problems was that youtube-dl wasn't working properly if located in C:\Program Files\, but works when located somewhere that doesn't require Administrator Privileges.

The --get-filename part requires an @ symbol before the youtube-dl path.


  1. youtube-dl gets filename from video and saves to var %%f
  2. youtube-dl downloads the video to C:\Path\To\Downloads\
  3. Merge output as mp4, so you can specify the extension for FFmpeg input
  4. FFmpeg converts video to webm and uses %%f as input/output filename.

for /f "delims=" %%f in ('
    @"C:\Users\Matt\Desktop\youtube-dl.exe" 
    --get-filename -o "%%(title)s" 
    "https://www.youtube.com/watch?v=TWNhqCHw0qc"
    ') 

do ("C:\Users\Matt\Desktop\youtube-dl.exe" 
    -f best "https://www.youtube.com/watch?v=TWNhqCHw0qc" 
    -o "C:\Users\Matt\Downloads\%%f.mp4" 
    --merge-output-format mp4 

    && 

    ffmpeg -y 

    -i "C:\Users\Matt\Downloads\%%f.mp4" 
    -c:v libvpx 
    -b:v 1300K -crf 16 
    -pix_fmt yuv420p 
    -map 0:v:0? 
    -sn 
    -c:a libvorbis 
    -q:a 6
    -map 0:a:0? 
    -f webm 
    "C:\Users\Matt\Downloads\%%f.webm"
   )