Variable Frame Rate MP4

From SDA Knowledge Base

Jump to: navigation, search

Contents

Introduction

This guide assumes you're comfortable with AviSynth, MeGUI and batch files or working through the command line.

What is variable frame rate? You can probably guess that a constant frame rate is where the frame rate of a video stays the same throughout its whole length. In VFR you'll have sections that have a different framerate. VFR is useful when the game you've recorded has multiple in-game framerates. For example, God of War for the PS2 outputs gameplay at full framerate while its cinematics are half framerate.


Understanding the VFR process

1. Normally, videos that have different framerates cannot be joined together.

Vfr1.png


2. Using AviSynth, we can change the framerates of each video so that they match, and can thus be joined. Of course this will make parts of the video play faster or slower than it should. As you can see, changing the framerate from 60 to 30 has doubled the length of Video2.

Vfr2.png


3. Once joined, a temporary video is encoded with H.264.

Vfr3.png


4. To get the video to play like normal again, we tell tc2mp4 which parts should be set to the original framerate. The result is a variable framerate video.

Vfr4.png


Files you'll need

Download tc2mp4 and extract the contents to your working directory. Place mp4box.exe in the same directory. Mp4box can be found in "..\MeGUI\tools\mp4box\".


Avisynth script

The first step is to extract the audio stream before we do any kind of video stretching. Here's a simple AviSynth script, modify it to your liking.

vid1=avisource("my30fpsvid.avi")
vid2=avisource("my60fpsvid.avi")

vid2=vid2.changefps(vid1)

AlignedSplice(vid1,vid2)
ConvertToYV12()
  • Load the script into VirtualDub(Mod), go to File -> Information and write down the video length. For the sake of this guide, the length of the above script is 3:28.167.
  • Encode the audio.


Once that's done it's time to prepare the real avisynth script. Simply change changefps to assumefps.

vid1=avisource("my30fpsvid.avi")
vid2=avisource("my60fpsvid.avi")

vid2=vid2.assumefps(vid1)

AlignedSplice(vid1,vid2)
ConvertToYV12()
  • Load the script into VirtualDub(Mod), go to File -> Information and write down the video length. For the sake of this guide, the length of the above script is 5:11.333.
  • Do not encode the video yet!


Calculating the correct video bitrates

Here's something you have to watch out for. Let's say that you're encoding a 10 minute long video at 2048 kbps that you plan to stretch to 20 minutes. What do you think would be the average bitrate for the new 20 minute vfr video? It wouldn't be 2048, it'd be 1024, since it's double the length.

A more extreme example: if a video is encoded at 2048 kbp/s and the video is 1 second long, the filesize will be 2048 kb.... right. What if it gets stretched out to 100 seconds? The filesize would still be 2048 kb, but the kbp/s will have changed to 20.48 kbp/s.

The point is that if you encode a video at the usual SDA bitrates, you will find that once the final stretching/shrinking has taken place, the average bitrate will be something unexpected.

On with it then... All you really need to calculate is one number, the ratio between the real length of the video and the length of the stretched/shrunk video. Taking the example above:

Real length = 3:28.167 = 208.167 seconds
Temporary length = 5:11.333 = 311.333 seconds

Ratio = 208.167 / 311.333 = 0.669

HQ 2048 * 0.669 = 1369 kbp/s
IQ 5000 * 0.669 = 3343 kbp/s

In the final stages when the temporary video gets stretched/shrunk back to the real length, the average bitrate will be the correct 2048/5000.

Now that you know the correct bitrate, go ahead and encode the temporary video.


Timecodes, finding the frame ranges

How difficult it is to find the frame ranges depends on the complexity of your AviSynth script. There's no easy way to do this, unless you're a scripting god, but you're already experienced enough that you should be able to figure it out.

An example: if a 29.97 fps video has 1000 frames, and a 59.94 fps video has 2500 frames, timecode.txt file will be:

# timecode format v1
Assume 29.97
0,999,29.97
1000,3499,59.94

Remember: 0 counts as a frame number.


Setting the VFR

Your encoding should be done now. Open a command line and use the following:

tc2mp4 -i myCFRvid.mp4 -t timecode.txt -o myVFRvid.mp4

tc2mp4 gets rid of any audio in your files so mux the one you made at the beginning of the guide.

And that's it! Now just make sure it plays correctly in Quicktime and you're good to go.

Personal tools