Issue using xargs in Linux

1

I am attempting to pipe the output from youtube-dl to xargs as I would like to name the downloaded file with the title extracted from the first command. however am having trouble doing so. The command I am using is as follows;

youtube-dl --get-title "Youtube URI" | xargs youtube-dl -o {} "YouTube URI"

PeanutsMonkey

Posted 2012-06-11T22:20:19.877

Reputation: 7 780

Xargs can't deal with spaces ' and " unless you pass the -0 flag. Also, I don't know youtube-dl, but maybe it doesn't take standard input, so xargs would be useless. – Bruno9779 – 2012-06-11T23:35:49.363

@Bruno9779 - What do you mean by space ' and "? How would I know whether an application like youtube-dl takes standard input? – PeanutsMonkey – 2012-06-11T23:54:06.187

I mean the space character, quotes and double quotes. Read the man pages of xargs for more on that and stdi/o (I don't want to paste all that here). It looks like youtube-dl takes only URLs as stdin, so your command can't work IMO. Still, try with a -0 flag after xargs first, you never know – Bruno9779 – 2012-06-12T00:56:27.483

@Bruno9779: -0 would produce a file name with the ending newline character in it, which is allowed, but often annoying. If no other delimiter is given with -d, xargs defaults to \n ("newline") which is appropriate in this case. – Daniel Andersson – 2012-06-12T07:06:17.320

Answers

3

To address your specific question about xargs, you need to specify that you want to use the {} placeholder:

youtube-dl --get-title "Youtube URI" |\
    xargs -I{} youtube-dl -o {} "Youtube URI"

See man xargs for more info, and what switches are also implied by -I.

Since it's only a single argument, it would be more straightforward to use substitution:

youtube-dl -o "$(youtube-dl --get-title "Youtube URI")" "Youtube URI"

Note that these constructions will give you a file without appropriate file extension.

The most straightforward way to accomplish your actual task would as mentioned be to use the built-in title naming option:

youtube-dl -t "Youtube URI"

as described in man youtube-dl. This will also produce the correct file extension for the video file.

Daniel Andersson

Posted 2012-06-11T22:20:19.877

Reputation: 20 465

Thanks. I don't quite follow what you mean by I need to specify that I want to use the {} placeholder. My understanding is that {} is a placeholder for the output produced in the preceding command. – PeanutsMonkey – 2012-06-14T00:31:39.613

@PeanutsMonkey: No, usually xargs just appends all incoming arguments after the given command. echo foo bar | xargs -d' ' echo baz will print baz foo bar on a single line; the arguments have been appended to a single echo call. The {} placeholder is a special construct, and needs to be explicitly activated with -I, as it says in the manual. – Daniel Andersson – 2012-06-14T08:05:18.520

2

I have been reading youtube-dl man pages: what you are trying to do is much simpler than that, you just need the -t,--title flag.

youtube-dl --title "Youtube URL"

or

youtube-dl -t "Youtube URL"

http://dev.man-online.org/man1/youtube-dl/

Bruno9779

Posted 2012-06-11T22:20:19.877

Reputation: 1 225

The reason I want to avoid youtube-dl --title "YouTube URL" is because it has a different output to that ofyoutube-dl --get-title "Youtube URL". – PeanutsMonkey – 2012-06-14T00:29:38.837