2

I know it's been asked before but I couldn't find a fast and accurate solution yet, I want to trim videos with a maximum lenght of 15 seconds. Here is what I've tried so far, I'm working with this video as example:http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_30mb.mp4

The fastest way

This is by far the fastest way because it doesn't need to re-encode the video but it's very innacurate in this video because the key frames are every 5-10 seconds:

ffmpeg -i SampleVideo_1280x720_30mb.mp4 -ss 30 -to 50 -c copy -y out.mp4

Here is how I check the key frames in this video:

ffprobe -select_streams v -show_frames -show_entries frame=pict_type -of csv SampleVideo_1280x720_30mb.mp4 | grep -n I | cut -d ':' -f 1

With this command I found out that the second key frame it's in the 211th frame which means 210 frames between the first key frame and the second, knowing that it's a 25fps video it would be around 8 seconds between the key frames, so any cut between 1-8 seconds would fall in one of this frames which is a huge difference for a 15 seconds video.

The accurate way

This way it's accurate but it requires to re-encode the video:

ffmpeg -i SampleVideo_1280x720_30mb.mp4 -ss 30 -to 50 -preset ultrafast -y out.mp4

The problem is this command takes more than 20 seconds to finish using the ultrafast preset, in a samsung galaxy s6 edge plus which probably would be much more in other devices.

Nevertheless, if I open the SampleVideo_1280x720_30mb.mp4 in Instagram and trim it they get an accurate trim in less than 2 seconds.

I also tried to force the keyframes every second, but it also requires to re-encode the video and it affects the video quality:

ffmpeg -i SampleVideo_1280x720_30mb.mp4 -force_key_frames "expr:gte(t,n_forced*1)" out.mp4

Does anybody know how Instagram trim the videos or a way to get a fast and accurate trim with FFMpeg?

dan87
  • 333
  • 3
  • 16
  • Where are you viewing the Instagram-trimmed files? – Gyan Jun 04 '16 at 05:31
  • I didn't check that but when I trim the video with the app it takes like 2 seconds and the trim it's accurate I don't know if they are actually trimming but using seek to doesn't work for the same reason – dan87 Jun 04 '16 at 07:45
  • How do you know the trim is accurate? Where are you viewing the trimmed files? – Gyan Jun 04 '16 at 07:46
  • I can see it in the preview but I don't see the files – dan87 Jun 04 '16 at 09:24
  • Preview in Instagram? Yeah, I don't think the files are actually getting trimmed. – Gyan Jun 04 '16 at 09:30
  • But how do they do that even if I use the player and seek to a second between 1-8 it jumps to the closest keyframe, is there a way to use the media player and seek to any frame – dan87 Jun 05 '16 at 00:55

1 Answers1

1

I figured out what Instagram does, there is basically no way to trim a video accurately faster. So in order to show the user a preview, I did the following:

  • When the user moves the thumb to set the new cut position I take a screenshot of the current image displayed by the videoplayer.

  • Put an ImageView the same size as the videoplayer on top of it with a spinner, so the videoplayer is no longer visible.

  • Mute the videoplayer and seek for the closest key-frame before the user's desired position.

  • Start playing the video from the key-frame and calculate the time from the key-frame to the desired position.

  • Hide the ImageView and unmute the video.

So in the video I used as an example, the first key-frame it's at second 0 and the second key-frame it's at second 8. Lets say the user wants to make a cut in the second 3, then I seek to the key-frame at the second 0 and wait for 3 seconds before unmuting and showing the video. This way the user only has to wait for 3 seconds before seeing the preview.

The real trim is made later in a background process, when the user accepts the preview.

dan87
  • 333
  • 3
  • 16
  • Do you mind looking at my question? I think you can help me. Link -https://stackoverflow.com/questions/54494510/ffmpeg-low-trim-accuracy – Rektirino Feb 03 '19 at 22:09