Difference between revisions of "Variable Frame Rate MP4"

From SDA Knowledge Base

Jump to: navigation, search
m
m (Understanding the VFR process)
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Introduction==
 
==Introduction==
  
This is an advanced guide meant for those who want that extra quality in their videos. Creating VFR video takes a lot more work than CFR. You should be 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. 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.
 
+
This guide uses the concatenation method meaning that you will have to split your video on each change in framerate. So if the framerate changes six times, you're going to create seven different videos. Now you might understand why I said this is a lot of work, you're going to have to do some serious AviSynth scripting! The drawback of the concatenation method is that the bitrate distribution when doing multi-pass will be affected negatively.
+
  
 
<br>
 
<br>
 +
===Understanding the VFR process===
  
==Setup==
 
  
There are two things you must do to make this work properly:
+
1. Normally, videos that have different framerates cannot be joined together.
* Encode to raw avc (*.264) in MeGUI/CLI.
+
* Each part absolutely must have the same framerate. If you used MvBob with NTSC material, use AssumeFPS(59.94) in all the avisynth scripts, including those that used Telecide.
+
  
Make it easier for yourself and name each .264 file with some kind of sequence. Place those files in a folder that contains MP4box.exe. You can locate it in "..\MeGUI\tools\mp4box\".
+
[[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>
  
==The batch files==
+
==Files you'll need==
  
If you don't want to use batch files, the mp4box commands look like this:
+
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\".
<pre><nowiki>
+
mp4box -add testvfr_tele.264 -fps 29.97 -new tele.mp4
+
mp4box -add testvfr_mvbob.264 -fps 59.94 -new mvbob.mp4
+
mp4box -cat tele.mp4 -cat mvbob.mp4 -new telebob.mp4
+
</nowiki></pre>
+
  
Otherwise, here are three batch files that should make this less tedious. Those PAL people out there, you know what to do.
+
<br>
 +
 
 +
==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.
  
<b>mp4_2997.bat</b>
 
 
<pre><nowiki>
 
<pre><nowiki>
@echo off
+
vid1=avisource("my30fpsvid.avi")
REM ### Change path below to your working directory ###
+
vid2=avisource("my60fpsvid.avi")
set mypath=C:\heymanyoubetterchangethis
+
  
:start
+
vid2=vid2.changefps(vid1)
if {%1}=={} goto :eof
+
 
set name=%1
+
AlignedSplice(vid1,vid2)
"%mypath%MP4Box.exe" -add %name% -fps 29.97 -new %name:~0,-5%.mp4
+
ConvertToYV12()
shift
+
goto start
+
 
</nowiki></pre>
 
</nowiki></pre>
  
<b>mp4_5994.bat</b>
+
* 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 <b>changefps</b> to <b>assumefps</b>.
 +
 
 
<pre><nowiki>
 
<pre><nowiki>
@echo off
+
vid1=avisource("my30fpsvid.avi")
REM ### Change path below to your working directory ###
+
vid2=avisource("my60fpsvid.avi")
set mypath=C:\heymanyoubetterchangethis
+
  
:start
+
vid2=vid2.assumefps(vid1)
if {%1}=={} goto :eof
+
 
set name=%1
+
AlignedSplice(vid1,vid2)
"%mypath%MP4Box.exe" -add %name% -fps 59.94 -new %name:~0,-5%.mp4
+
ConvertToYV12()
shift
+
goto start
+
 
</nowiki></pre>
 
</nowiki></pre>
  
<b>mp4_cat.bat</b>
+
* 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.
<pre><nowiki>
+
* Do not encode the video yet!
@echo off
+
REM ### Change path below to your working directory ###
+
set mypath=C:\heymanyoubetterchangethis
+
  
if {%2}=={} goto :eof
+
<br>
"%mypath%MP4Box.exe" -cat %1 -cat %2 -new "%mypath%joined.mp4"
+
 
shift
+
==Calculating the correct video bitrates==
shift
+
 
:start
+
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.
if {%1}=={} goto :eof
+
 
"%mypath%MP4Box.exe" -cat "%mypath%joined.mp4" -cat %1 -new "%mypath%joined.mp4"
+
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.
shift
+
 
goto start
+
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:
 +
 
 +
<pre><nowiki>
 +
# timecode format v1
 +
Assume 29.97
 +
0,999,29.97
 +
1000,3499,59.94
 
</nowiki></pre>
 
</nowiki></pre>
 +
 +
<font color="red"><b>Remember:</b></font> 0 counts as a frame number.
  
 
<br>
 
<br>
  
==Creating the VFR video==
+
==Setting the VFR==
  
Whichever .264 files were encoded with Telecide, select and drag them onto the mp4_2997.bat batch file. This will create a new set of mp4 files. Do the same with the MvBob'ed files but on mp4_5994.bat.
+
Your encoding should be done now. Open a command line and use the following:
  
Once they've all been muxed into an mp4, drag that set onto the mp4_cat.bat batch file. This will join everything into one big file. Hopefully you won't get any errors. If you do, complain on the [http://speeddemosarchive.com/yabb/YaBB.pl?board=other Tech Support forum].
+
<pre><nowiki>
 +
tc2mp4 -i myCFRvid.mp4 -t timecode.txt -o myVFRvid.mp4
 +
</nowiki></pre>
  
I didn't mention anything about audio.. did I? I suggest creating the audio track from the regular avisynth script for your segment, before all the splitting, and muxing that into the joined 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.
 +
<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