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?

1 Answer 1


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)


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.

  • Thanks. I've used ffmpeg and sed to get duration and fps and thus made a csv sheet and was generating random integer to pick the frame, but this is much better.
    – RAVI
    Commented Jun 4, 2018 at 17:56
  • But the audio.m4a is not 1 second length. I'm not able to ascertain how long it will be. It is a bit random, sometimes going as long as 30 seconds. I've tried with -to value+1, but that gave me error. Finally something like this ffmpeg -i in.mp4 -ss 2 -to 3 -map 0:a -c:a copy audio.m4a -vf thumbnail -vframes 1 img.png is giving me 1 sec audio, but the frame I get with this code is different to the one where I give -ss in the input, besides -ss in input does the fast seek. So is there a way to use fast seek -ss and still get 1 sec audio.
    – RAVI
    Commented Jun 4, 2018 at 19:13
  • Share full log of my command run.
    – Gyan
    Commented Jun 4, 2018 at 19:26
  • log image. imgur.com/a/U0ylzsu
    – RAVI
    Commented Jun 4, 2018 at 20:03
  • I'll have to look into this, but for now, you can drop -c:a copy and it should be accurate
    – Gyan
    Commented Jun 5, 2018 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.