How Do I Do Math Inside an Ffmpeg/Youtube-Dl Command or Use -to While Having -Ss Before -I

Using youtube-dl and ffmpeg I've got downloading part of a youtube video working fine thanks to this question

ffmpeg -ss 60 -i $(youtube-dl -g -f 18 "") -t 5 -c copy out.mp4

but I'd like to simplify figuring out the -t argument. Currently I do "copy video URL at current time" twice on youtube and then with a calculator do "ending second - starting second" to get the correct length. So far I'm thinking that I should either

  • somehow just paste that calculation in

so something like

ffmpeg -ss 60 -i $(youtube-dl -g -f 18 "") -t $(expr 65 - 60) -c copy out.mp4

but in a way that actually works. Looked at question about doing basic math in bash but it doesn't really cover how to do math when giving an argument.

Or

  • figure out a way to use -to instead -t in a way that doesn't first download the entire video
ffmpeg -i $(youtube-dl -g -f 18 "") -ss 60 -to 65 -c copy out.mp4

Works fine for short videos but as the 1st link at some point mentions not having -ss before -i results in downloading the entire video before it's replaced with a cut one which is far from ideal for longer videos(ie. ffmpeg -i $(youtube-dl -g -f 22 "") -ss 38347 -to 38838 -c copy 2022-04-04_PPJT45_720p.mp4) Based on explanation here I thought using -ss twice might work but no luck

ffmpeg -ss 60 -i $(youtube-dl -g -f 18 "") -ss 00 -to 65 -c copy out.mp4

65s clip so basically same as using -t

ffmpeg -ss 60 -i $(youtube-dl -g -f 18 "") -ss 60 -to 65 -c copy out.mp4

interestingly this did a 5s clip at 120s.

At this point I feel rather stuck and not really sure what more I could do to figure out how to do what I want.

In case it's relevant I'm currently using Git BASH that Git for Windows includes to do the commands.

Edit: Simply placing -to before the -i works

ffmpeg -ss 60 -to 65 -i $(youtube-dl -g -f 18 "") -c copy out.mp4

Still wouldn't mind knowing how to do the other option but for now I'm happy.

2
Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article