picking a frame and an overlapping audio from a video I know how to extract frames and audio from a video using ffmpeg.

But what I want to is, to pick a random frame in that video, and then picking a random 1 second audio clip that overlaps in time with the sampled frame.

As of now I'm doing this by preprocessing the videos for frames and audio, but that is consuming a lot of disk-space.

The way I'm doing it now is: I extract the frames and audio before hand I'm finding the number of frames and picking one from them randomly. By finding the name of that frame, which gives me it's number, say 100th out of 3000 frames, I'm picking audio which overlaps with it.

So is there a easier way of achieving this?
The inverse way of doing this is faster.

Get file duration

ffprobe in.mp4 -show_entries format=duration -of compact=p=0:nk=1 -v 0


This will print the duration in fractional seconds.

Pick a random value between (0, duration - 1)

Run

ffmpeg -ss value -t 1 -i in.mp4 -map 0:a -c:a copy audio.m4a -vf thumbnail -vframes 1 img.png


The thumbnail filter will pick one frame out of the 1 second of video provided to it.