Difference between revisions of "Variable Frame Rate MP4"

From SDA Knowledge Base

Jump to: navigation, search
(Setting the VFR)
m (Understanding the VFR process)
 
(22 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
This guide assumes you're comfortable with AviSynth, MeGUI and batch files or working through the command line.
 
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. MvBob is best suited for F1 while Telecide is best suited for F2.
+
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.
  
Creating the actual VFR MP4 isn't so hard, the tedious part is setting up the AviSynth script and frame ranges. You have to look through your segment for changes in framerate and get the exact frame that this happens.
+
<br>
 +
===Understanding the VFR process===
 +
 
 +
 
 +
1. Normally, videos that have different framerates cannot be joined together.
 +
 
 +
[[Image: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.
 +
 
 +
[[Image:vfr2.png]]
 +
 
 +
 
 +
3. Once joined, a temporary video is encoded with H.264.
 +
 
 +
[[Image: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.
 +
 
 +
[[Image:vfr4.png]]
  
 
<br>
 
<br>
  
==File you'll need==
+
==Files you'll need==
  
Download [[Media:tc2mp4_20060613_pom20061203.zip|tc2mp4]] and extract the contents to your working directory.
+
Download [[Media:tc2mp4_20060613_pom20061203.zip|tc2mp4]] and extract the contents to your working directory. Place mp4box.exe in the same directory. Mp4box can be found in "..\MeGUI\tools\mp4box\".
  
 
<br>
 
<br>
  
==Encoding the AviSynth script==
+
==Avisynth script==
  
Besides finding the frame ranges, the idea is that we make the parts with Telecide match the framerate of the parts with MvBob. The telecide parts will play at double the speed in the CFR version, but we will use tc2mp4 to slow them down which will get us our VFR version. Your script should be similar to this:
+
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.
  
 
<pre><nowiki>
 
<pre><nowiki>
Loadplugin("C:\Program Files\DGMPGDec\DGDecode.dll")
+
vid1=avisource("my30fpsvid.avi")
 +
vid2=avisource("my60fpsvid.avi")
  
Ac3Source(MPEG2source("SEG7_5155.d2v"),"SEG7_5155_DELAY-73ms.ac3").DelayAudio(-0.073)
+
vid2=vid2.changefps(vid1)
  
assumetff()
+
AlignedSplice(vid1,vid2)
 +
ConvertToYV12()
 +
</nowiki></pre>
  
a=last.Trim(0,1720).mvbob()
+
* 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.
b=last.Trim(1721,4080).telecide().assumefps(a)
+
* Encode the audio.
c=last.Trim(4081,11480).mvbob()
+
d=last.Trim(11481,12455).telecide().assumefps(a)
+
e=last.Trim(12456,13485).mvbob()
+
f=last.Trim(13486,16680).telecide().assumefps(a)
+
g=last.Trim(16681,17200).mvbob()
+
  
AlignedSplice(a,b,c,d,e,f,g)
+
 
Lanczos4Resize(640,480)
+
Once that's done it's time to prepare the real avisynth script. Simply change <b>changefps</b> to <b>assumefps</b>.
 +
 
 +
<pre><nowiki>
 +
vid1=avisource("my30fpsvid.avi")
 +
vid2=avisource("my60fpsvid.avi")
 +
 
 +
vid2=vid2.assumefps(vid1)
 +
 
 +
AlignedSplice(vid1,vid2)
 
ConvertToYV12()
 
ConvertToYV12()
 
</nowiki></pre>
 
</nowiki></pre>
  
Encode it!
+
* 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!
  
 
<br>
 
<br>
  
==Finding the frame ranges==
+
==Calculating the correct video bitrates==
  
To calculate the frame ranges from the AviSynth script above, first find out how many there are in each set. Since MvBob is a bobbing deinterlacer, it doubles the amount of frames. Don't forget that the first number in the trim command is inclusive. So the above sets are 3442,2360,14800,975,2060,3195,1040, that's 27872 frames in total.
+
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<br>
 +
Temporary length = 5:11.333 = 311.333 seconds
 +
 
 +
Ratio = 208.167 / 311.333 = 0.669
 +
 
 +
HQ 2048 * 0.669 = 1369 kbp/s<br>
 +
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.
 +
 
 +
<br>
 +
 
 +
==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:
  
So, timecode.txt would be:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
# timecode format v1
 
# timecode format v1
Assume 29.970030
+
Assume 29.97
0,3441,59.940060
+
0,999,29.97
3442,5801,29.970030
+
1000,3499,59.94
5802,20601,59.940060
+
20602,21576,29.970030
+
21577,23636,59.940060
+
23637,26831,29.970030
+
26832,27871,59.940060
+
 
</nowiki></pre>
 
</nowiki></pre>
<font color="green"><b>Tip:</b> Write down the left hand side first by incrementing from the set above, then the end frame is just the one before.</font>
+
 
 +
<font color="red"><b>Remember:</b></font> 0 counts as a frame number.
  
 
<br>
 
<br>
Line 65: Line 114:
 
==Setting the VFR==
 
==Setting the VFR==
  
Your CFR encoding should be done now. Open a command line and use the following:
+
Your encoding should be done now. Open a command line and use the following:
  
 
<pre><nowiki>
 
<pre><nowiki>
Line 71: Line 120:
 
</nowiki></pre>
 
</nowiki></pre>
  
Now you can mux the audio in.
+
tc2mp4 gets rid of any audio in your files so mux the one you made at the beginning of the guide.
  
Et voilà, you're done.
+
And that's it! Now just make sure it plays correctly in Quicktime and you're good to go.
 +
<br>

Latest revision as of 02:00, 31 December 2007

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