Vfr處理

出自NMM Doc
跳至導覽 跳至搜尋

AviSynth, 可變幀速率(vfr)和混合視頻(hybrid video)

根據幀速率可將視頻分為兩種類型,固定幀速率(CFR)的視頻和可變幀速率(VFR)的視頻.CFR視頻幀速率為恆定值,VFR視頻幀速率為非恆定值.因為AVI不支持VFR所以許多視頻編輯程序(例如VirtualDub和AviSynth)都假設VFR視頻的幀速率為恆定值.由於一系列的原因,這在不久的將來也是不會有改變的.雖然AVI容器不支持VFR,但有一些容器(例如MKV,MP4和WMV/ASF)支持VFR.

混合視頻通常定義為一個混合着pulldown和non-pulldown(pulldown可以是場,標準的3:2 pulldown,或full frame)的視頻.這與pulldown是否是hard(在回放時 場field/幀frame 重複)或soft(流stream中加入這標記表明在播放時哪些 場field/幀 frame是重複的)無關.所以,它可以是CFR或VFR.因此,混合視頻是簡單地混合着不同的幀速率(如通常用於動畫的8,12和16fps)原始幀率base framerate是pulldow之前的速率的視頻.這種視頻需要根據內容決定最終的幀速率.

可變幀速率和混合視頻


我們需要知道通常視頻都是CFR的.但是混合視頻轉換成VFR是很常見的VFR的例子. 混合視頻是混合着不同原始幀速率base framerate的視頻 (如通常用於動畫的8,12和16fps). 更常見的混合視頻的例子是由部分隔行掃描interlaced/progressive NTSC (29.97 fps)和部分FILM (t通過膠捲過帶將23.976 fps 轉成29.97 fps)組成的視頻. soft pulldown, NTSC的部分(也叫做視頻video部分)回放速度是29.97 fps和添加重複幀膠捲過帶telecined的部分(23.976 fps轉換至29.97 fps). hard pulldown,沒有增加任何場fields且回放速度是29.97. 另一個混合視頻的例子是現代電視動畫,科幻電視劇例如SG1,星級迷航,TNG和巴比倫5,和很多紀錄片DVD.

TIVTC工具被設計出用來用於無損的處理混合視頻,Decomb工具則用於通過合成來將視頻處理成CFR.

如何分辨VFR視頻(mkv/mp4)


這裏有一些判斷mkv/mp4是否是VFR的方法:

mpeg-2: DGIndex可以檢查Film/Video的比例, 它可以告訴你有多少內容是soft pulldown的. 它不能處理hard pulldown, 它也不能準確的識別視頻當視頻中混合着soft pulldown和hard pulldown.

mkv: 用mkv2vfrmkvtoolnix取出timecodes.txt文件.

mp4: 用mp4dump(from the MPEG4 tools by MPEG4ip package). 打開命令行節目並輸入(使用相對路徑)

mp4dump -verbose=2 holly_xvid.mp4 > log.txt

打開log文件,你會看到類似下面的代碼(看stts部分來找出每一幀的長度):

type stts
     version = 0 (0x00)
     flags = 0 (0x000000)
     entryCount = 41 (0x00000029)
      sampleCount = 3 (0x00000003)
      sampleDelta = 1000 (0x000003e8)
      sampleCount[1] = 1 (0x00000001)
      sampleDelta[1] = 2000 (0x000007d0)
      sampleCount[2] = 3 (0x00000003)
      sampleDelta[2] = 1000 (0x000003e8)
      sampleCount[3] = 1 (0x00000001)
      sampleDelta[3] = 2000 (0x000007d0)
      etc ...

sampleDelta顯示幀frames的長度,sampleCount顯示有多少幀frames. 根據上面的例子我們得到如下數據: 3幀frames顯示長度1000 1幀frames顯示長度2000 3幀frames顯示長度1000 1幀frames顯示長度2000 .......... 顯示長度的值的單位不是秒而是滴答"ticks", 你可以通過時間表"timescale"來計算時間. 時間表"timescale"被存儲在視頻軌track里(確定你看的是正確的軌, 因為每個軌都有它自己的時間表timescale). 找這樣的輸出內容:

   type mdia
   type mdhd
   ...
   timeScale = 24976 (0x00006190)
   duration = 208000 (0x00032c80)
   language = 21956 (0x55c4)
   reserved = <2 bytes> 00 00

在本例中時間表timeScale是24976. 大多數幀frames的顯示長度是1000. 1000/24976 = 0.04這意味着前3幀frames每一幀顯示了0.04秒,與25 fps (1/25 = 0.04)是相同的. 下一個幀的顯示長度是2000. 2000/24976 = 0.08這意味着這一幀顯示了0.08秒, 與12.5 fps (1/12.5 = 0.08)是相同的. etc ... 從上面例子中的log文件可以看出這個視頻是混合視頻

在AviSynth中打開MPEG - 2混合視頻並重新編碼

假設你有混合視頻,這裏有列出幾種方法對它進行編碼.第一種方法是將它轉換為cfr視頻(23.976 fps或29.97 fps). 第二中方法是編碼成120 fps的avi並且丟幀dropped frames (其中重複的幀null幀將被丟棄後播放). 第三中方法是使用mkv或mp4容器創建一個真正的vfr.

編碼至CFR(23.976 fps或29.970 fps)


(翻譯中) If we choose the video rate, the video sequences will be OK, but the FILM sequences will not be decimated, appearing slightly jumpy (due to the duplicated frames). On the other hand, if we choose the FILM rate, the FILM sequences will be OK, but the video sequences will be decimated, appearing jumpy (due to the "missing" frames). Additionally, when encoding to 29.97 fps, you will get lower quality for the same file size, because of the 25% greater number of frames. It's a tough decision which to choose. If the clip is mostly FILM you might choose 23.976 fps, and if the clip is mostly video you might choose 29.97 fps. The source also is a factor. If the majority of the video portions are fairly static "talking heads", for example, you might be able to decimate them to 23.976 fps without any obvious stutter on playback.

When you create your d2v project file you will see whether the clip is mostly video (NTSC) or FILM (in the information box). However, many of these hybrids are encoded entirely as NTSC, with the film portions being "hard telecined" (the already telecined extra fields having also been encoded) so you'll have to examine the source carefully to determine what you have, and how you wish to treat it.

The AviSynth plugins Decomb and TIVTC provide two special decimation modes to better handle hybrid clips by blending. This will eat bitrate quickly, but it appears very smooth. Here is a typical script to enable this mode of operation:

Telecide(order=0, guide=1)
Decimate(mode=X) # tweak "threshold" for film/video detection

or

TFM(mode=1)
TDecimate(mode=0,hybrid=X) # tweak "vidThresh" for film/video detection

There are 2 factors that enable Decimate to treat the film and nonfilm portions appropriately. First, when Telecide declares guide=1, it is able to pass information to Decimate about which frames are derived from film and which from video. For this mechanism to work, Decimate must immediately follow Telecide. Clearly, the better job you do with pattern locking in Telecide (by tweaking parameters as required), the better job Decimate can do.

The second factor is the threshold. If a cycle of frames is seen that does not have a duplicate, then the cycle is treated as video. The threshold determines what percentage of frame difference is considered to be a duplicate. Note that threshold=0 disables the second factor.

Make sure to get the field order correct - DVDs are generally order=1, and captured video is generally order=0. The included DecombTutorial?.html explains how to determine the field order.

Mostly Film Clips (mode=3)

When the clip is mostly film, we want to decimate the film portions normally so they will be smooth. For the nonfilm portions, we want to reduce their frame rate by blend decimating each cycle of frames from 5 frames to 4 frames. Video sequences so rendered appear smoother than when they are decimated as film. Set Decimate to mode=3, or TDecimate to hybrid=1 for this behavior.

Another IVTC was developed specifically to handle hybrid material without blended frames: SmartDecimate. While you do get "clean" frames as a result, it also may play with slightly more stutter than does Decomb's result. A typical script might go:

B = TDeint(mode=1) # or KernelBob(order=1)
SmartDecimate(24, 60, B)

In order to keep the result as smooth playing as possible, it will insert the "Smart Bobbed" frames from time to time.

Mostly Video Clips (mode=1)

When the clip is mostly video, we want to avoid decimating the video portions in order to keep playback as smooth as possible. For the film portions, we want to leave them at the video rate but change the duplicated frames into frame blends so it is not so obvious. Set Decimate to mode=1, or TDecimate to hybrid=3 for this behavior.

In this case you may also consider leaving it interlaced and encoding as such, especially if you'll be watching on a TV later.

編碼至cfr - 120 fps

你需要TIVTC和avi_tc. 首先創建 已經刪幀decimate過的avi和timecodes.txt, 不要muxing. 然後打開tc-gui的tc2cfr標籤並添加你的文件或這使用以下命令行 :

tc2cfr 120000/1001 c:\video.avi c:\timecodes.txt c:\video-120.avi

然後和你的音頻一起mux. 它的工作原理是通過tc2cfr創建一個沒有丟幀frames的avi並插入null幀來創建一個平滑的120fps avi.

編碼至VFR (mkv)

首先下載 mkvtoolnix. 我們將會用它將我們的視頻和時間碼(timecode)封裝入MKV容器. 請使用最新版本 (在翻譯本文時最新的版本為4.4.0), 某些舊版本無法正確處理timecode.

There are several AviSynth plugins that you can use to generate the VFR video and required timecode file. An example is given below using the Decomb521VFR plugin. Another alternative is the TDecimate plugin contained in the TIVTC package. See their respective documentations to learn more about tweaking them.

The DeDup plugin removes duplicate frames but does not change the framerate (leaving jerky video if not decimated first), so it won't be included. It can still be used after either method by using their timecodes as input to DeDup.

Decomb521VFR:

Add this to your script:

Decomb521VFR_Telecide(order=1, guide=0)
Decomb521VFR_Decimate(mode=4, threshold=1.0, progress=true, \
             timecodes="timecodes.txt", vfrstats="stats.txt")

Open this script in VirtualDub, it will create the timecodes and stats files, then encode. It will seem to freeze at first, because it examines every frame on the first load.

TIVTC:

This is a 2-pass mode. Add this to your script:

TFM(mode=1, output="tfm.txt")
TDecimate(mode=4, output="stats.txt")

Open this and play through it in VirtualDub. Then close it, comment those lines out (or start a second script) and add:

TFM(mode=1)
TDecimate(mode=5, hybrid=2, dupthresh=1.0, input="stats.txt", \
          tfmin="tfm.txt", mkvout="timecodes.txt")

Load and encode.

framerate:

If you're encoding to a specific size using a bitrate calculator, vfr decimation will mess up the calculations. To make them work again add these to your script:

Before decimation:

oldcount = framecount # this line must be before decimation
oldfps = framerate

End of script:

averagefps = (float(framecount)*float(oldfps))/float(oldcount)
AssumeFPS(averagefps)

封裝:

現在封裝成MKV:

  1. 打開mmg.exe (mkvmerge的圖形界面)
  2. 添加你的視頻流文件
  3. 添加你的音頻流文件
  4. 點擊已經導入的視頻流軌道
  5. 導入你的 "timecodes.txt" 時間碼文件timecode file
  6. 點擊你的音軌
  7. 如果你的音軌需要延遲delay,設置它
  8. 開始封裝

要播放它你需要Matroska分離器splitter. 播放AVC你需要Haali's分離器Splitter,播放ASP你可以用Gabest's分離器Splitter.

編碼至vfr(mp4)

如果你創建了一個帶null幀的120 fps avi視頻 然而在你將它mux成mp4的時候,null幀將被刪除. 更那啥的方法是轉換多個cfr avi文件 (一些23.976 fps的film和一下29.97 fps的vide) 然後用mp4box和-cat 參數將他們打包成一個vfr mp4文件.

第三, 更簡單的,方法是使用上面mkv的方法然後用tc2mp4處理視頻:你可以在 Doom9 論壇找到更多有關tc2mp4的信息.

方法總結

總結上述方法的優點與缺點. 如果我們編碼至23.976或29.97 fps的cfr(像AviSynth和Virtualdub需要的那樣)將會使它可能在回放時看起來很慫=口=因為重複幀和丟失了幀. 我們可以通過blending來迴避它, 但是編碼器不能很好的處理它. 如果編碼至含有null幀的120 fps視頻,它是cfr的,並且在回放時不會看起來很慫,也很適合編輯. 編碼至使用timecdoes的真正的vfr MKV不損失也不重複幀,得不到想avi那樣的廣泛支持 I.

在Avisynth中打開非MPEG-2混合視頻並重編碼

在Avisynth中打開vfr視頻並保持同步是可能的:DirectShowSource.大多數常見的格式都支持混合視頻(vfr)例如mkv, mp4, wmv,和 rmvb,並且下列方法皆可用於它們;不過,如果源是mkv,你也可是使用 mkv2vfr 和AviSource.

在Avisynth中打開非avi vfr內容

最好的保持所以幀frames都同步和準確的方法是將其轉換成恆定幀速率,例如24/30可以轉換成120 (或者119.88). 不要忘記convertfps=true,它可以像ChangeFPS那樣添加幀frames,或者你的音頻將不同步.

DirectShowSource("F:\Hybrid\vfr.mp4", fps=119.88, convertfps=true)

你可以將它當作30P來打開但之後你要重刪幀re-decimate,只要選一部分就行了,例如24P,損失30P的部分: 重編碼至23.976fps或29.97 fps:

# fps=29.97 或 fps=23.976
DirectShowSource("F:\Hybrid\vfr.mkv", \
                 fps=29.97, convertfps=true)

或者

DirectShowSource("F:\Hybrid\vfr_startrek.mkv", \
                 fps=119.88, convertfps=true)
FDecimate(29.97) # or FDecimate(23.976)

另一種方法是找出平均幀速率framerate (用總幀數frames除以總長度的秒數)並在DirectShowSource中使用它. 根據幀frame的時間, 幀frames將被添加或刪除來保持同步, 這種方法一定會卡卡=口=. DirectShowSource不會膠捲過帶.

重編碼120 fps視頻

最簡單的在Avisynth中將vfr源轉換回vfr的方法是使用DeDup:

1st pass:

DupMC(log="stats.txt")

2nd pass:

DeDup(threshold=.1, maxcopies=4, maxdrops=4, dec=true, \
      log="stats.txt", times="timecodes.txt")

TIVTC 也可以做到:

1st pass:

TFM(mode=0,pp=0)
TDecimate(mode=4, output="stats.txt")

2nd pass:

TFM(mode=0,pp=0)
TDecimate(mode=6, hybrid=2, input="stats.txt", mkvout="timecodes.txt")

如果你編碼了你的文件,用mkv或120fps avi來mux它.

這將刪除所有重複的幀,同時保持總幀數framecount和時間幾乎相同. 但是不要使用來自輸入視頻的timecode文件,使用新的,因為它們可能不相同. (如果你想使用dedup的更多功能,你可以嘗試其他參數.)

通過Avisynth轉換vfr至cfr

你可以通過專門的工具省略掉分析和刪幀decimate的步驟直接得到一個最小恆定幀速率的avi來餵avisynth.經過處理和重新編碼,使用tc2cfr或mmg輸出並通過添加timecodes來還原vfr並完全同步,. (如果你執行了刪幀decimate或改變幀速率,你就只能自己編輯timecode文件了, 雖然dedup有一個timesin參數.)

avi

avi_tc 如果avi沒有使用丟幀drop frames和非n-vops或完全編碼的幀frames.本工具將創建一個timecode和普通視頻.它要求沒有音頻或第二個track. 要使用它, 打開tc-gui並且添加你的文件或者使用以下命令:

cfr2tc c:\video-120.avi c:\video.avi c:\timecodes.txt 1

mkv

mkv2vfr 從Matroska中提取所有視頻幀frames轉換成普通avi文件和timecode文件.只有在mkv是vfw-mode時才工作. 用以下命令行實現它:

mkv2vfr.exe input.mkv output.avi timecodes.txt

轉換至MPEG-2 vfr 視頻

http://forum.doom9.org/showthread.php?t=93691

本文段落作者還沒有看貼, 所以這只是他的copy/paste.

音頻同步

我們已經討論過轉換你手中視頻的幾個方法(23.976 fps, 29.97 fps的cfr視頻或者vfr視頻). 你可能想知道為什麼不論你用的方法來編碼你的視頻你的音頻總是保持同步. 在此之前的編碼, 視頻和音頻擁有相同的長度, 所以他們始終同步. 你可能會遇到下面兩種情況:

  • 你改變幀速率framerate使流stream變快或變慢(通常在PAL-FILM的轉換中). 這意味着視頻流的長度被改變了,因此音頻流出現不同步.
  • 你通過增加和減少幀frames改變了幀速率framerate.這意外這視頻的長度仍然相同, 因此音頻流仍然同步.

如果你使用下列代碼將23.976 fps的視頻編碼至29.97 fps(同為cfr)

Decimate(mode=3, threshold=1.0) 或
Decimate(mode=1, threshold=1.0)

幀frames將被刪除或增加,因此你的音頻流會保持同步. 因為我們可以推理得出vfr編碼時也會同步.

最後, 假設你在Avisynth中通過DriectShowSource打開vfr視頻. 比較下方

# fps=29.97 或 fps=23.976
DirectShowSource("F:\Hybrid\vfr_startrek.mkv", fps=29.97)

# fps=29.97 or fps=23.976
DirectShowSource("F:\Hybrid\vfr_startrek.mkv", fps=29.97, convertfps=true)

前者將因為24p提速而出現音頻不同步, 而後者因為增加了幀frames所以音頻仍然同步.

參考

原始出處avisynth VFR
必讀: Force Film, IVTC, and Deinterlacing and more (doom9的好人們寫的).
如何創建 120 fps video.
說明文檔 Decomb521VFR.
關於 Decomb521VFR1.0 Matroska VFR自動處理模塊.
Mkvextract GUI by DarkDudae.

Besides all people who contributed to the tools mentioned in this guide, the author of this tutorial (Wilbert) would like to thank bond, manono, tritical and foxyshadis for their useful suggestions and corrections of this tutorial.

翻譯:btcdtc