I'm trying to write a Bash script that would download an audio track from youtube and convert it to Apple ringtone format, which is .m4r. The tools I employ are youtube-dl
and ffmpeg
. The former seems to work fine but I have a strange issue with the latter. When I try to pass the name of the file parametrically, shell prints that file or directory can't be found.
I use the same method for unwraping user input (or my defaults) for both commands but it only seems to work with youtube-dl
.
Example of what doesn't work and print the text above. Obviously, all the files are in fact there and accessible
youtube-dl -i --extract-audio --audio-format m4a -o $filepath_interim --audio-quality 0 $video_link
ffmpeg -i $filepath_interim -acodec copy -f ipod -ss $offset -t $length $filepath_out
So, I tried to do it with ''
strings, $(command)
syntax, it all failed. I only found one way to make it work, which is to hard code the values of the paths but this defeats the whole purpose of my script.
youtube-dl -i --extract-audio --audio-format m4a -o $filepath_interim --audio-quality 0 $video_link
ffmpeg -i ~/Downloads/ringtone.m4a -acodec copy -f ipod -ss $offset -t $length ~/Downloads/ringtone.m4r
I want to figure out why this keeps happening, whether it is in any way specific to ffmpeg
or am I just missing some piece of knowledge about $
name unwrapping.
Minimal Working Example. Run with bash or pack in script and run. If your path to bash
is different, please change the shebang:
#!/usr/local/bin/bash
# initialise
filepath_interim="~/Downloads/ringtone.m4a"
filepath_out="~/Downloads/ringtone.m4r"
video_link="https://www.youtube.com/watch?v=dQw4w9WgXcQ"
offset=0
length=30
# main part
youtube-dl -i --extract-audio --audio-format m4a -o $filepath_interim --audio-quality 0 $video_link
ffmpeg -i $filepath_interim -acodec copy -f ipod -ss $offset -t $length $filepath_out
~
to $HOME
See ShellCheck and SC2088: Tilde (~) does not expand in quotes, use $HOME for more info.