Log in

View Full Version : How to change the framerate of an MP4 file without re-encoding video?


MikeBell
21st January 2016, 23:32
Hi all,

I have a poorly encoded video of a webcast (I presume it came from a WebEx screencast) and the main issue is that the framerate of the mp4 file is double than it should be. If I play it with VLC and slow down the playback speed to 0.5x, everything's normal and I can understand the audio. If I play it at 1x, I can't understand anything and everything's too fast.

Mediainfo says this about the file:

General
Complete name : source.mp4
Format : MPEG-4
Format profile : Base Media
Codec ID : isom
File size : 181 MiB
Duration : 33mn 53s
Overall bit rate mode : Constant
Overall bit rate : 748 Kbps
Writing application : Lavf56.19.100

Video
ID : 1
Format : MPEG-4 Visual
Format profile : Simple@L1
Format settings, BVOP : No
Format settings, QPel : No
Format settings, GMC : No warppoints
Format settings, Matrix : Default (H.263)
Codec ID : 20
Duration : 33mn 53s
Bit rate mode : Constant
Bit rate : 674 Kbps
Width : 640 pixels
Height : 480 pixels
Display aspect ratio : 4:3
Frame rate mode : Variable
Frame rate : 10.000 fps
Minimum frame rate : 9.990 fps
Maximum frame rate : 29.970 fps
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Compression mode : Lossy
Bits/(Pixel*Frame) : 0.220
Stream size : 163 MiB (90%)
Writing library : Lavc52.108.0

Audio
ID : 2
Format : AAC
Format/Info : Advanced Audio Codec
Format profile : LC
Codec ID : 40
Duration : 33mn 53s
Duration_LastFrame : -19ms
Bit rate mode : Constant
Bit rate : 70.2 Kbps
Channel(s) : 2 channels
Channel positions : Front: L R
Sampling rate : 48.0 KHz
Compression mode : Lossy
Stream size : 17.0 MiB (9%)

My first stab at this problem was to use mp4box to change the framerate to 5fps. I actually succeeded at "slowing" down the video but I failed at slowing down the audio. Here's what I tried:

$ mp4box -add source.mp4\#video -raw 1 -new video


this created "video_track1.cmp" file.

$ mp4box -add source.mp4\#audio -raw 2 -new audio

this created "audio_track2.aac" file.

I then tried combining them with:

$ mp4box -add video_track1.cmp:fps=5 -add audio_track2.aac:fps=5 -new dest.mp4

This actually partially worked and produced a dest.mp4 that had video portion correct and it played at the right speed but the audio was still playing too fast:

General
Complete name : dest.mp4
Format : MPEG-4
Format profile : Base Media
Codec ID : isom
File size : 181 MiB
Duration : 1h 7mn
Overall bit rate mode : Variable
Overall bit rate : 373 Kbps
Encoded date : UTC 2016-01-21 22:24:30
Tagged date : UTC 2016-01-21 22:24:30

Video
ID : 1
Format : MPEG-4 Visual
Format profile : Simple@L1
Format settings, BVOP : No
Format settings, QPel : No
Format settings, GMC : No warppoints
Format settings, Matrix : Default (H.263)
Codec ID : 20
Duration : 1h 7mn
Bit rate mode : Variable
Bit rate : 337 Kbps
Maximum bit rate : 929 Kbps
Width : 640 pixels
Height : 480 pixels
Display aspect ratio : 4:3
Frame rate mode : Constant
Frame rate : 5.000 fps
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Compression mode : Lossy
Bits/(Pixel*Frame) : 0.220
Stream size : 163 MiB (90%)
Title : cmp:fps=5@GPAC0.5.1-DEV-rev5375
Writing library : Lavc52.108.0
Encoded date : UTC 2016-01-21 22:24:30
Tagged date : UTC 2016-01-21 22:24:55

Audio
ID : 2
Format : AAC
Format/Info : Advanced Audio Codec
Format profile : LC
Codec ID : 40
Duration : 33mn 53s
Bit rate mode : Variable
Bit rate : 70.2 Kbps
Maximum bit rate : 87.8 Kbps
Channel(s) : 2 channels
Channel(s)_Original : 3 channels
Channel positions : Front: L C R
Sampling rate : 48.0 KHz
Compression mode : Lossy
Stream size : 17.0 MiB (9%)
Title : aac:fps=5@GPAC0.5.1-DEV-rev5375
Encoded date : UTC 2016-01-21 22:24:52
Tagged date : UTC 2016-01-21 22:24:55

You can see above that Audio track's duration is wrong.

How do I fix the audio track? I don't mind re-encoding it with ffmpeg if that's the only option. I don't know how to do it however. Also, can mp4box do this on its own? What am I doing wrong?

raffriff42
22nd January 2016, 00:19
FFMpeg: How to speed up / slow down a video (https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video) Using a complex filtergraph, you can speed up [or slow down] video and audio at the same time:

ffmpeg -i input.mkv -filter_complex (https://ffmpeg.org/ffmpeg-filters.html#Filtergraph-description) "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mkv

Avisynth (http://avisynth.nl/index.php/Main_Page) is another option, handy for fixing all sorts of things like this, on the fly (you play the script directly, without re-encoding) DirectShowSource (http://avisynth.nl/index.php/DirectShowSource)("input.mkv")
AssumeFPS (http://avisynth.nl/index.php/AssumeFPS)(5, sync_audio=true)

StainlessS
22nd January 2016, 00:34
Nice one Raff, Archiving your post.

Yesterday, encoded MP4 @ Aspect Ratio 240:1, was a bit of a struggle to re-AR @ 2.40:1

Is there a standard ffmpeg fix for that (there is a special ffmpeg version that re-does AR, somewhere on site).
(I used YAMB + MeGUI, about 6 tries to get correct).

EDIT: Some links on changing AR

http://forum.doom9.org/showthread.php?t=152419
http://forum.videohelp.com/threads/316259-Can-I-change-the-pixel-aspect-ratio-in-an-x264-AVI-without-reencoding
http://forum.videohelp.com/threads/301849-Change-Aspect-Ratio-of-mp4-without-re-encoding

MikeBell
22nd January 2016, 01:11
FFMpeg: How to speed up / slow down a video (https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video)

Avisynth (http://avisynth.nl/index.php/Main_Page) is another option, handy for fixing all sorts of things like this, on the fly (you play the script directly, without re-encoding) DirectShowSource (http://avisynth.nl/index.php/DirectShowSource)("input.mkv")
AssumeFPS (http://avisynth.nl/index.php/AssumeFPS)(5, sync_audio=true)

I tried this:

ffmpeg -i source.mp4 -filter_complex "[0:a]atempo=0.5[a]" -map "[a]" outputaudio.m4a

and this... which is a simplified, audio-only, option:

ffmpeg -i out.m4a -filter:a "atempo=0.5" -vn outputaudio.aac


Note: I used 0.5 because 2.0 speeds it up 2x and I want to slow it down 2x.

Result: audio sounds horrible :( It has that weird metallic sounding echo all over the track. It sounds nothing like what you hear when you slow down to 0.5x with VLC.

I'm still unsure how to proceed...

Nice one Raff, Archiving your post.

Yesterday, encoded MP4 @ Aspect Ratio 240:1, was a bit of a struggle to re-AR @ 2.40:1

Is there a standard ffmpeg fix for that (there is a special ffmpeg version that re-does AR, somewhere on site).
(I used YAMB + MeGUI, about 6 tries to get correct).

EDIT: Some links on changing AR

http://forum.doom9.org/showthread.php?t=152419
http://forum.videohelp.com/threads/316259-Can-I-change-the-pixel-aspect-ratio-in-an-x264-AVI-without-reencoding
http://forum.videohelp.com/threads/301849-Change-Aspect-Ratio-of-mp4-without-re-encoding


Several times I managed to change aspect ratio without re-encoding by doing this:

ffmpeg -i in.mp4 -vcodec copy -acodec copy -aspect 16:9 output.mp4

MikeBell
22nd January 2016, 01:21
My previous post ended up in moderation queue because I quoted StainlessS's post with lots of links ;)

Anyway, abreviation:

raffriff42: I tried this:

ffmpeg -i out.m4a -filter:a "atempo=0.5" -vn outputaudio.aac
same as:
ffmpeg -i source.mp4 -filter_complex "[0:a]atempo=0.5[a]" -map "[a]" outputaud.m4a


and it sounded horrible. While the speed sounded correct, it had that metallic echo all over it. Couldn't understand anything. It sounded nothing like VLC's 0.5x slowdown. Note: I used 0.5 and not 2.0 like in your example because I wanted it to slow down, not speed up by 2x.

StainlessS: I always try changing AR like this... without re-encoding. Most of the time it works great:

ffmpeg -i in.mp4 -vcodec copy -acodec copy -aspect 16:9 output.mp4