5

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 date and 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.

Any suggestions?

Clarification

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.

The 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.

2
  • Why? Out of curiosity... what's it for?
    – Dagelf
    Commented Apr 8, 2020 at 7:29
  • The 5 digit string goes roughly from 10 hours ago to 20 hours ago. I can not understand, nor make any sense of the 10:10 A.M. value, nor the "previous midnight". Without clarification this question can not be answered.
    – user232326
    Commented Apr 8, 2020 at 9:33

1 Answer 1

3

I didn't get what you got with your date command, so I might be completely wrong about what you want.

To generate a time between 00:00:20 and 10:10:00 with GNU date, you can do:

start time:

$ date --date "1970-01-01 00:00:20" '+%T'
00:00:20

end time (36580 seconds later):

$ date --date "1970-01-01 00:00:20 36580 sec" '+%T'
10:10:00

something in between:

date --date "1970-01-01 00:00:20 $(shuf -n1 -i0-36580) sec" '+%T'
2
  • I like this (third) variant better than the one I came up with. My variation depends on my script's trigger time, which can have some slight fluctuation. This one is more static and so the range for the semi-random time signature being generated can be more precise. Thanks for offering it.
    – MJiller
    Commented Apr 8, 2020 at 13:16
  • 1
    FYI: I only added the date 1970-01-01 to prevent wrong results caused by daylight savings. Omitting the date string and adding option --utc has the same effect.
    – Freddy
    Commented Apr 8, 2020 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .