How do I use youtube-dl's --add-header option?
Asked Answered
B

2

8

I'm trying to add a custom header using youtube-dl, a popular video downloader with command line interface.

I'm using PowerShell (or CMD) on Windows 10.

The official documentation says like the following but I can't seem to use it properly.

--add-header FIELD:VALUE
Specify a custom HTTP header and its value, separated by a colon ':'. You can use this option multiple times

I'm trying to add multiple headers for the request like:

"Accept-Encoding": "identity;q=1, *;q=0",
"Range": "bytes=6488064-",
"Referer": "https://avideosite.net/video/0123456",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36"

But when I tried something like

start youtube-dl --add-header "Accept-Encoding":"identity;q=1, *;q=0" --add-header "Range":"bytes=6488064-" --add-header "Referer":"https://avideosite.net/video/0123456" --add-header "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36" "http://11.22.333.444:8280/abcdefg=.mp4?st=97WbFiADB5Hla7Y-fZx58g&e=1560574126"

It doesn't work and throws an error like this:

Start-Process : A positional parameter cannot be found that accepts argument
'Accept-Encoding'.
At line:1 char:1
+ start youtube-dl --add-header "Accept-Encoding":"identity;q=1, *;q=0" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

What am I doing wrong?

Also, is there a proper way to put it into Python script using youtube_dl library?

Brave answered 15/6, 2019 at 8:50 Comment(5)
try removing startAmphibious
@SiddharthDas That'll only give an error youtube-dl : The term 'youtube-dl' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + youtube-dl + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (youtube-dl:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundExceptionBrave
That means either youtube-dl is not correctly installed or path is not added to enviroment variables.Amphibious
The installation instructions say "Windows users can download an .exe file and place it in any location on their PATH except for %SYSTEMROOT%\System32". Did you do that?Vituline
Oops, I didn't see that. Now I added it to my path and $youtube-dl command works like a charm.Brave
B
11

So my problem was not having youtube-dl.exe in my PATH, which prevented me from even starting youtube-dl. So let me answer my own question about --add-header option.

About --add-header option, it should be something like foo:"bar" for each item.

For example, my original command from the question should be like:

$ youtube-dl --add-header Accept-Encoding:"identity;q=1, *;q=0" --add-header Range:"bytes=6488064-" --add-header Referer:"https://avideosite.net/video/0123456" --add-header User-Agent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36" http://11.22.333.444:8280/abcdefg=.mp4?st=97WbFiADB5Hla7Y-fZx58g&e=1560574126

Remember that if you have &(ampersand) character in the url like in my case, you'll have to wrap it with " ".

Brave answered 15/6, 2019 at 11:49 Comment(2)
I think the syntax is youtube-dl --add-header 'Accept-Encoding:identity;q=1, *;q=0' instead of youtube-dl --add-header Accept-Encoding:"identity;q=1, *;q=0".Vanillin
@AeroWang: that depends entirely on your shell. Accept-Encoding:"<header value>" is mostly the same as 'Accept-Encoding:<header value>', provided there are no shell variable expansions in the double-quoted section. Single quoted strings are not parsed for shell variables. Unquoted strings without whitespace or shell expansions are concatenated to quoted strings, and the example here has no shell expansions. So for the example in this answer, there is no difference for youtube-dl.Imponderabilia
B
4

Adding an additional answer here because I cannot comment yet. The accepted answer suggests using the "--add-header" option in youtube-dl to add a referer in the header. I couldn't get this to work but did have sucess using the "--referer" option which does the same thing. example usage is as follows:

youtube-dl --referer https://exampleVideoHost.com/hostURLWhereEmbeddedVideoAppears https://exampleVideoHost.com/videoSRCvalue
Biography answered 1/12, 2019 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.