I want to generate a somewhat random time signature in a bash script using a variable. I've got a video file that's 11 hours long and I'll be muxing a 50-minute mp3 podcast that I download most weekdays with the video using
ffmpeg's -shortest option (that will trim the video to be the length of the much shorter audio). So I want the audio to get inserted into the video each day at a random point not later than 50 minutes before the video's end.
What I've come up with so far utilizes the
shuf commands. Using
shuf I generate a randomish 5-digit string between 36305 and 72850 and feed that to
date as follows:
date --date="$(shuf -i 36305-72850 -n 1) sec ago" +%T
(the 5-digit string is within the range, in seconds, between 10:10 A.M., on the one hand, and the previous midnight, on the other--10 A.M. being 36305 seconds and midnight being 72805 seconds before my script runs). This seems to work fine to generate the needed time stamp, but I suspect there are alternative ways of generating the time stamp that may be more elegant and/or optimal.
The semi-random time stamp I'm trying to generate is something that gets fed to
ffmpeg using the
-ss switch, and must take the form of 3 numeric couplets separated by colons and conforming to time-tracking conventions (for practical purposes related to this query numeric limitations on the couplets are 0-24 for the first couplet and 0-60 for the second two--though for my usage the first couplet should be 0-10 and the second 0-10 if the first couplet is 10, otherwise 0-60, and the final couplet 0-60). The
date utility is already designed in conformance with these time-tracking constraints, which is why I thought of using some variation on
date +%T first.
ffmpeg command I'm running is something like the following
ffmpeg -ss 02:54:32 -i 11-hr_video.mp4 -i todays_50-min_audio.mp3 -codec copy -shortest muxed_video-audio.mp4
I need a semi-random value to insert after
-ss and the
date incantation I offered does that sort of thing. So, as an example using, at around 8 P.M. (the time when my script runs most days), that incantation, the
ffmpeg command would look something like the following:
ffmpeg -ss $(date --date="$(shuf -i 36305-72850 -n 1) sec ago" +%T) -i 11-hr_video.mp4 -i todays_50-min_audio.mp3 -codec copy -shortest muxed_video-audio.mp4
I'm creating a variable in my script to store the semi-random value being generated. So I have in my script something like
starttime=$(date --date="$(shuf -i 36305-72850 -n 1) sec ago" +%T)
My script is triggered at around 8 P.M. most days so subtracting the specified number of seconds gets a time stamp close to the previous midnight, on the one hand, and a second close to 10:10 A.M., on the other. Thus, a roughly 10 hour 10 minute span from within which to semi-randomly select a start time where I can begin muxing the audio into the video.
Hope this clarification helps. Btw, I like Freddy's incantation better than the one I proposed, so I'll probably be using it instead.