Jump to content

Batch job for youtube-dl


Recommended Posts

Ok, I've been inspired by the Hak5 podcast to give youtube-dl a shot.

I've got youtube-dl and ffmpeg on my Win7 machine, and I'm working on a .bat to make it easier to use, but I'm running into errors when I try to run my .bat.

Here's the setup: I've put the ffmpeg, youtube-dl, an input text file, and the .bat into a folder called YTDL. Everything works fine if I open a cmd prompt in the YTDL folder and run...

.\youtube-dl --ffmpeg-location %CD%\ffmpeg\bin -a input.txt -x --audio-format mp3 --audio-quality 128K -o %CD%\output\%(title)s.%(ext)s 

It puts the lotion in the basket downloads the video, strips the audio to an .mp3, and deletes the downloaded file. With that working, I thought I'd make a .bat so I don't have to copy and paste the command in, but it just won't work. The cmd window pops open and closes with no indication of what's wrong. It doesn't do anything beyond that. No download, no new file, nothing. If I open a cmd prompt in the folder and call the .bat or drag it in and hit enter, I get the same thing. No difference whether running CMD as admin or normal user.

I've built a weekly maintenance batch file that runs without issue, but that was calling programs known natively to windows. I have no other experience to build upon, so I'm at a loss. I'd very much appreciate some help figuring this out so I can check off the practical exercises from my Hak5 lessons :)

My question is; what on earth am I doing wrong?

Link to comment
Share on other sites

My suspicion is the %cd% before your ffmpeg path. Unless you are using a script section we don't see which uses a variable for %cd%, but this windows shortcut path does not exist. "%cd%" ie: %appdata% is a real path, but %cd% is not

edit: I read the %cd% wrong

If using ".\" make sure the exe is in the same directory as the bat script. Otherwise, remove the ".\" if it's in the default windows path, which will allow it to run from anywhere.

Try using quotes around paths to your binaries too if long paths with spaces in the names, ie:

youtube-dl --ffmpeg-location  "d:\directory with long path names and spaces\ffmpeg\bin\" -a input.txt -x --audio-format mp3 --audio-quality 128K -o "d:\my path to output\some folder\%(title)s.%(ext)s "

Some apps might want the output paths written the way you would see in an html file too, so the last section might need forward slashes. Check the documentation or just experiment with the output. Cooper's pause at the end should show you errors as you debug the process though.

Edited by digip
Link to comment
Share on other sites

Ok, so here's what happens.. Double-click the .bat or drag into the cmd window, I get this...

D:\YTDL>youtube-dl --ffmpeg-location "D:\YTDL\ffmpeg\bin" -a input.txt -x --audio-format mp3 --audio-quality 128K -o "D:\YTDL\output\(ext)s"   & pause
Usage: youtube-dl [OPTIONS] URL [URL...]

youtube-dl: error: Cannot download a video and extract audio into the same file! Use "D:\YTDL\output\(ext)s.%(ext)s" instead of "D:\YTDL\output\(ext)s" as the output template
Press any key to continue . . .

and of course that's a fail. However, if I copy the contents of batch.bat and paste them into cmd, I get this...

D:\YTDL>.\youtube-dl --ffmpeg-location "%CD%\ffmpeg\bin" -a input.txt -x --audio-format mp3 --audio-quality 128K -o "%CD%\output\%(title)s.%(ext)s" & pause
[youtube] V0aqCYdiIjc: Downloading webpage
[youtube] V0aqCYdiIjc: Extracting video information
[youtube] V0aqCYdiIjc: Downloading DASH manifest
[download] Destination: D:\YTDL\output\Discovering And Enumerating Targets - Metasploit Minute.m4a
[download] 100% of 14.63MiB in 00:12
[ffmpeg] Correcting container in "D:\YTDL\output\Discovering And Enumerating Targets - Metasploit Minute.m4a"
[ffmpeg] Destination: D:\YTDL\output\Discovering And Enumerating Targets - Metasploit Minute.mp3
Deleting original file D:\YTDL\output\Discovering And Enumerating Targets - Metasploit Minute.m4a (pass -k to keep)
Press any key to continue . . .

Thanks for your help capturing the output from the failed attempts. Before adding the quotes I would get an error regarding the name of the downloaded and extracted files having same name, IF I tried to run the batch. As above, pasting the batch contents directly to the cmd window did not yield the same. Instead, it worked a treat every time. What I'm most concerned with is getting the batch job to work. I'd love to be able to open a cmd from start/win+r/anywhere and run this, but as of yet I've still got to open cmd "in" the ytdl folder.

Link to comment
Share on other sites

Say, why is it that in your -o parameter you have "%(title)s.%(ext)s" ? Currently the command prompt tries to substitute in the value of the variable named "(title)s." which, since it doesn't exist, is an empty string.

If you expect ffmpeg to substitute in variables like "%(title)" and "%(ext)" you must first prevent the command prompt from interpreting them.

Edited by Cooper
Link to comment
Share on other sites

If in your bat file you literally have a > then remove it and put a \ instead:


change D:\YTDL>youtube-dl to "D:\YTDL\youtube-dl" with quotes (if the executable is not in a windows path config or put in c:\windows\system32)

Your bat file above doesn't have the % sign variables that cooper pointed out in his post. ffmpeg needs those(if you have them, then ignore, but I imagine if multple files, it's going to overwrite the same file every time because of this)

Link to comment
Share on other sites

The title and extension strings are there because the output files were saved as "output#" with no extension. Just running youtube-dl {URL} the video would be saved with a proper title and extension, but no dice when making an mp3 from video. Don't know why, but that's what it's been doing to me.

The > is not in the batch. I'm having to open cmd "in" the folder where this all resides, so that's just the command prompt smiling at me. Or poking at me. Still not sure about that.

I tried fiddling with it some more, pulling out arguments such as no-cache and others, just to see if it would run. Of course that gets nowhere. Still in same boat, but at least it's got oars; what? Well, I can still copy & paste the contents of the bat into the cmd window and it runs fine. Also did some poking around for an extension/plu-in for chrome or "gasp" IE to add urls to the text file. Got close, but couldn't save to a file of my choosing. Also got distracted for a little while with a couple machines today, but at least my workstation is up again, there's no customer equipment on the bench, and I can somewhat more comfortably bang my head against THAT surface when my eyes start bleeding again ;)

I'll keep pluggin' away at it. I'll post my findings if I have a "eureka" moment. In the meantime I'll keep my eye on the thread to see if there are any golden nuggets to pick.

Link to comment
Share on other sites

It's a shame there isn't a way for the plugin to write all the video URLs to a file for you, and you could then just pass everything to wget to grab them and demux them locally with ffmpeg after you have the raw flv files and such. Still, this is better than manually doing it with something like wireshark to look for the video file and then manually grab the URL and download it. This could probably be scripted to do the same process using other tools as well, such as tshark or tcpdump if you feed it a list of video links and spider the pages, could probably get it to find the FLV files. I had written something for downloading mp3's and avi's from RSS feeds a while back, might still be on the forums, which I used to download all the older hak5 episodes before just watching them streamed now since internet is so much faster, but I could re-run the script and it would ignore files I already had, and pickup where I left off on partials I had already started and it only used wget and the RSS feed. Compiling the links for stuff off youtube would probably not be as easy as that since the RSS had links to all the main files I wanted to play, but could probably be done.

Link to comment
Share on other sites

Tried your edit, Cooper, but the caret just shows up in the filename and extension, probably because of the quotes. The quotes got there when I first started with this, when the folder path had some spaces, but I never took them out after moving the working directory. Take out the quotes and it's just like not having the caret. Other than that, no difference. As before, trying to run the batch still fails, but pasting it's contents into a cmd window "in" the folder containing youtube-dl works.

I think it's time for a new approach at this....

I think I first need to solve why youtube-dl will only run from a cmd window opened from the directory in which it resides.

Then if it still quits with errors I can work on that.

So, to get cmd (opened from anywhere) to recognize youtube-dl as a program, should I put youtube-dl (and ffmpeg,ffprobe, etc.) into a windows system folder somewhere, or add the folder they're in to PATH, or something else altogether?

Link to comment
Share on other sites

You can move it into the default path so it loads from anywhere by putting it into the windows\system32 directory, but I tend to like keeping them out of the system folder and giving them their own home since this isn't a default windows sys file. To do the latter, right click my computer > properties > advanced system settings > environmental variables> and then under the path sections, you can paste in quotes preceding with a semi-colon, the path to the youtube exe. (not including the file name)

There are two sections, at the top for the "user" variables, so this is only available to you, or also system variables. If you put the actual file in winows\system32, then every user on the machine has access to the file, which you may not want if you have shared accounts on the same box, which for example, my mother has her own computer with multiple people who have their own sign-ons, if I want only my mother to be able to use an app that say , we don't want my neice to use, we would do it this way which allows me to set it up under her account, while others can't access it.

I do this on my own computer with wget and nmap in windows so I can use it from anywhere while not allowing anyone else to have access to these tools, and if I want to easily update wget or add bat files for these tools, i put them in the wget folder as well, and i can then run them from the command line anywhere. Then all I have to do is overwrite wget.exe any time I need to put in an update.

This is personal preference, but just an idea.

Edited by digip
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...