<p>The inverse way of doing this is faster.</p>

<p>Get file duration</p>

<pre><code>ffprobe in.mp4 -show_entries format=duration -of compact=p=0:nk=1 -v 0
</code></pre>

<p>This will print the duration in fractional seconds.</p>

<p>Pick a random value between (0, duration - 1)</p>

<p>Run</p>

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

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

CC BY-SA 4.0
2018-06-04T17:56:10.003Z
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
CC BY-SA 4.0
2018-06-04T19:13:17.610Z
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
CC BY-SA 4.0
2018-06-04T19:26:35.050Z
Share full log of my command run.
Gyan
CC BY-SA 4.0
2018-06-04T20:03:53.433Z
log image. https://imgur.com/a/U0ylzsu
RAVI
CC BY-SA 4.0
2018-06-05T05:46:59.933Z
I'll have to look into this, but for now, you can drop `-c:a copy` and it should be accurate
Gyan
CC BY-SA 4.0
2018-06-05T10:21:19.507Z
Thanks. I understand that without the `-c:a copy` it has to re-encode. But this is more accurate than with `-ss` in the output. This is giving exactly one second audio.
RAVI
CC BY-SA 4.0
2018-06-04T15:02:22.743Z
2018-06-04T15:02:22.743Z
Gyan
<p>I know how to extract frames and audio from a video using ffmpeg.</p>

<p>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.</p>

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

<p>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.</p>

<p>So is there a easier way of achieving this?</p>

<ffmpeg><audio>
picking a frame and an overlapping audio from a video
RAVI
# picking a frame and an overlapping audio from a video

(Asked by: username_0 on 2018-06-04)

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?

**Tags:** ffmpeg, audio


---

## 1 answers

### Answer by username_1 on 2018-06-04. Score: 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)
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.

> **Comments:**
> **username_0**: 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.  
> **username_0**: 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.  
> **username_1**: Share full log of my command run.  
> **username_0**: log image. https://imgur.com/a/U0ylzsu  
> **username_1**: I'll have to look into this, but for now, you can drop `-c:a copy` and it should be accurate  
> **username_0**: Thanks. I understand that without the `-c:a copy` it has to re-encode. But this is more accurate than with `-ss` in the output. This is giving exactly one second audio.  

---

