Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{} substitution #5667

Open
sharethewisdom opened this issue May 10, 2015 · 7 comments
Open

{} substitution #5667

sharethewisdom opened this issue May 10, 2015 · 7 comments

Comments

@sharethewisdom
Copy link

@sharethewisdom sharethewisdom commented May 10, 2015

youtube-dl --verbose --write-description -x --audio-format mp3 --audio-quality 3 -o "~/music/yt/%(uploader)s/%(title)s.%(ext)s" -k --exec "ytdir=~/music/yt/`printf "yt%(%m-%d)T\n"`; if [ ! -d $ytdir ]; then mkdir -p $ytdir; fi; cd $ytdir; ln {} ./; ln {}.description ./" https://youtu.be/mh8L9Bfk30k
[debug] System config: []
[debug] User config: []
[debug] Command-line args: ['--verbose', '--write-description', '-x', '--audio-format', 'mp3', '--audio-quality', '3', '-o', '~/music/yt/%(uploader)s/%(title)s.%(ext)s', '-k', '--exec', 'ytdir=~/music/yt/yt05-10; if [ ! -d /home/bart/music/yt/yt05-10 ]; then mkdir -p /home/bart/music/yt/yt05-10; fi; cd /home/bart/music/yt/yt05-10; ln {} ./; ln {}.description ./', 'https://youtu.be/mh8L9Bfk30k']
[debug] Encodings: locale UTF-8, fs utf-8, out UTF-8, pref UTF-8
[debug] youtube-dl version 2015.04.17
[debug] Python version 3.4.3 - Linux-3.19.3-3-ARCH-x86_64-with-arch
[debug] exe versions: ffmpeg 2.6.2, ffprobe 2.6.2, rtmpdump 2.4
[debug] Proxy map: {}
[youtube] mh8L9Bfk30k: Downloading webpage
[youtube] mh8L9Bfk30k: Extracting video information
[youtube] mh8L9Bfk30k: Downloading DASH manifest
[info] Writing video description to: /home/bart/music/yt/Cantica Symphonia/Anonimo (XIV secolo) Codice di Ivrea Almisonis melos Cantica Symphonia.mp4.description
[debug] Invoking downloader on 'https://[...]'
[download] Destination: /home/bart/music/yt/Cantica Symphonia/Anonimo (XIV secolo) Codice di Ivrea Almisonis melos Cantica Symphonia.mp4
[download] 100% of 19.64MiB in 00:06
[debug] ffmpeg command line: ffprobe -show_streams '/home/bart/music/yt/Cantica Symphonia/Anonimo (XIV secolo) Codice di Ivrea Almisonis melos Cantica Symphonia.mp4'
[ffmpeg] Destination: /home/bart/music/yt/Cantica Symphonia/Anonimo (XIV secolo) Codice di Ivrea Almisonis melos Cantica Symphonia.mp3
[debug] ffmpeg command line: ffmpeg -y -i '/home/bart/music/yt/Cantica Symphonia/Anonimo (XIV secolo) Codice di Ivrea Almisonis melos Cantica Symphonia.mp4' -vn -acodec libmp3lame -q:a 3 '/home/bart/music/yt/Cantica Symphonia/Anonimo (XIV secolo) Codice di Ivrea Almisonis melos Cantica Symphonia.mp3'
[exec] Executing command: ytdir=~/music/yt/yt05-10; if [ ! -d /home/bart/music/yt/yt05-10 ]; then mkdir -p /home/bart/music/yt/yt05-10; fi; cd /home/bart/music/yt/yt05-10; ln '/home/bart/music/yt/Cantica Symphonia/Anonimo (XIV secolo) Codice di Ivrea Almisonis melos Cantica Symphonia.mp3' ./; ln '/home/bart/music/yt/Cantica Symphonia/Anonimo (XIV secolo) Codice di Ivrea Almisonis melos Cantica Symphonia.mp3'.description ./
ln: failed to access ‘/home/bart/music/yt/Cantica Symphonia/Anonimo (XIV secolo) Codice di Ivrea Almisonis melos Cantica Symphonia.mp3.description’: No such file or directory
ERROR: Command returned error code 1
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/youtube_dl/YoutubeDL.py", line 1492, in post_process
keep_video_wish, info = pp.run(info)
File "/usr/lib/python3.4/site-packages/youtube_dl/postprocessor/execafterdownload.py", line 26, in run
'Command returned error code %d' % retCode)
youtube_dl.utils.PostProcessingError: Command returned error code 1

When I download somefile.mp4 and convert it to somefile.mp3, {}.description would be replaced by /full/path/somefile.mp3.description where I want the /full/path/somefile.mp4.description file.

I tried using --exec "echo $(basename {} .mp3).mp4.description" to strip/add the extension (and the path) but it got interpreted as echo {}.description:

[debug] Command-line args: ['--verbose', '--write-description', '-x', '--audio-format', 'mp3', '--audio-quality', '3', '-o', '~/music/yt/%(uploader)s/%(title)s.%(ext)s', '-k', '--exec', 'ytdir=~/music/yt/yt05-10; if [ ! -d /home/bart/music/yt/yt05-10 ]; then mkdir -p /home/bart/music/yt/yt05-10; fi; cd /home/bart/music/yt/yt05-10; ln {} ./; echo {}.description', 'https://youtu.be/mh8L9Bfk30k']

Storing {} in a variable naturally renders it being empty. I tried to use substitution and all sorts of escaped chars in a number of ways, but the problem seems to be that the {} string is being replaced with the converted (audio) file's full path when bash already tried to run its substitution routines.

A solution for this would be to automatically rename description files to match the {} in {}.description with {}. Please share your thoughts, I'd be interested to now if and how I'd be able to use substitution with {}.

@sharethewisdom sharethewisdom changed the title request: automatic .description file renaming / `{}` substitution request: automatic .description file renaming / {} substitution May 10, 2015
@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented May 10, 2015

Post the command you are using and the output with --verbose option

@dstftw
Copy link
Collaborator

@dstftw dstftw commented May 10, 2015

Provide some examples (with --verbose output) of what are you trying to achieve for better understanding.

@sharethewisdom
Copy link
Author

@sharethewisdom sharethewisdom commented May 10, 2015

sorry, I updated my original comment.

@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented May 10, 2015

I tried using --exec "echo $(basename {} .mp3).mp4.description" to strip/add the extension (and the path) but it got interpreted as echo {}.description:

That's because you are using double quotes and bash executes basename {} .mp3 before calling youtube-dl, it works with single quotes: --exec 'echo $(basename {} .m4a).mp4.description'.

I will look at the rest of the issue, but since your command is quite complex I would recommend writing a script that accepts the filename as the first argument and call it with --exec './script-name.sh {}' (you can even add it to your $PATH if you use it often).

@sharethewisdom
Copy link
Author

@sharethewisdom sharethewisdom commented May 10, 2015

bash executes basename {} .mp3 before calling youtube-dl

I will follow your recommendations. Could you elaborate on how/ when bash calls youtube-dl? I'm quite new to bash. I was wrapping the whole command in single quotes for defining an alias (that's how the escaping confusion began).
My original intent was to raise the question if a description file output template would be desirable. But as I went on writing, I figured this is probably a non-issue for most people.

@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented May 10, 2015

Could you elaborate on how/ when bash calls youtube-dl?

youtube-dl takes the argument after --exec, replace {} with the filename and runs the resulting command. That's all it does.
The problem is that the argument it receives may not be what you expect, because your shell will transform it in some cases. For example if you run --exec "echo $HOME", what youtube-dl sees is --exec "echo /home/<your-username>", but if you used --exec "echo $HOME" it will get --exec 'echo $HOME' (I recommend you to look into the Command-line args line in the verbose output).
So you have to use single quotes for everything you want to pass to youtube-dl directly and only use double quotes when you want the shell to expand something.

Pedantic note: It actually calls /bin/sh (which in a lot of systems is bash), but that's not too relevant (although it may affect you if you expect it to load your .bash_profile or similars, because it doesn't source them).

My original intent was to raise the question if a description file output template would be desirable. But as I went on writing, I figured this is probably a non-issue for most people.

I thinks the current behaviour of using title-id.description is reasonable, since we don't have to care if some postprocessor produces a file with a different extension, the same filename is used.

@sharethewisdom sharethewisdom changed the title request: automatic .description file renaming / {} substitution {} substitution May 10, 2015
@sharethewisdom
Copy link
Author

@sharethewisdom sharethewisdom commented May 10, 2015

Thanks i have a working script now.
This answer helped me understand how to use literal single quotes within the alias assignment: alias ytmp3='youtube-dl --exec '"'"'ytMusic.sh {}'"'"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.