“Vfr处理”的版本间的差异

来自NMM Doc
跳到导航 跳到搜索
第255行: 第255行:
 本文段落作者还没有看贴, 所以这只是他的copy/paste.
 本文段落作者还没有看贴, 所以这只是他的copy/paste.


== Audio synchronization ==
== 音频同步 ==


Several methods are discussed to encode your video (at 23.976, 29.97 or vfr video). You might wonder why your audio stays in sync regardless of the method you used to encode your video. Prior to encoding, the video and audio have the same duration, so they start out in sync. The following two situations might occur:
我们已经讨论过转换你手中视频的几个方法(23.976 fps, 29.97 fps的cfr视频或者vfr视频). 你可能想知道为什么不论你用的方法来编码你的视频你的音频总是保持同步. 在此之前的编码, 视频和音频拥有相同的长度, 所以他们始终同步. 你可能会遇到下面两种情况:


* you change the framerate of the stream by speeding it up or slowing it down (as is often done by PAL-FILM conversions). This implies that the duration of the video stream will change, and hence the audio stream will get out of sync.
* 你改变帧速率framerate使流stream变快或变慢( 通常在PAL-FILM 的转换中). 这意味着视频流的长度被改变了, 因此音频流出现不同步.


* you change the framerate of your the stream by adding or removing frames. This implies that the duration of the video stream will remain the same, and hence the audio stream will be in sync.
* 你通过增加和减少帧frames改变了帧速率framerate. 这意外这视频的长度仍然相同, 因此音频流仍然同步.


If you encode the video stream at 23.976 or 29.97 fps (both cfr) by using
如果你使用下列代码将23.976 fps的视频编码至29.97 fps( 同为cfr)


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


frames will be removed or added, and thus your audio stream will be in sync. By a similar reasoning the vfr encoding will be in sync.
帧frames将被删除或增加, 因此你的音频流会保持同步. 因为我们可以推理得出vfr编码时也会同步.


Finally, suppose you open vfr video in AviSynth with DirectShowSource. Compare the following
最后, 假设你在Avisynth中通过DriectShowSource打开vfr视频. 比较下方


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


and


  # fps=29.97 or fps=23.976
  # fps=29.97 or fps=23.976
  DirectShowSource("F:\Hybrid\vfr_startrek.mkv", fps=29.97, convertfps=true)
  DirectShowSource("F:\Hybrid\vfr_startrek.mkv", fps=29.97, convertfps=true)
The former will be out of sync since 24p sections are speeded up, and the latter will be in sync since frames are added to convert it to cfr.
前者将因为24p提速而不同步, 而后者因为增加了帧frames仍然同步.


== 引用 ==
== 引用 ==

2010年10月30日 (六) 15:08的版本

AviSynth, 可变帧速率(vfr)和混合视频(hybrid video)

根据帧速率可将视频分为两种类型,固定帧速率(CFR)的视频和可变帧速率(VFR)的视频.CFR视频帧速率为恒定值,VFR视频帧速率为非恒定值.许多视频编辑程序(例如VirtualDub和AviSynth)都假设VFR视频的帧速率为恒定值因为AVI不支持VFR.由于一系列的原因,这在不久的将来也是不会改变的.虽然AVI容器不支持VFR,但有一些容器(例如MKV,MP4和WMV/ASF)支持VFR.

混合视频通常定义为一个混合着pulldown和non-pulldown(pulldown可以是场,标准的3:2 pulldown,或full frame)的视频.这与pulldown是否是硬性(在回放时 场field/帧frame 重复)或软性(流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 (胶卷过带将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的比例, 它可以告诉你有多少内容是软性pulldown的. 它不能处理硬性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.

encoding to cfr - 120 fps

For this you'll need TIVTC and avi_tc. Start by creating a decimated avi with timecodes.txt, but skip the muxing. Then open tc-gui's tc2cfr tab and add your files or use this command line:

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

Then mux with your audio. This works because tc2cfr creates an avi with drop frames filling in the extra space with drop frames to create a smooth 120 fps avi.

encoding to vfr (mkv)

First download mkvtoolnix. We will use this to mux our video into the MKV container WITH a timecode adjustment file. Make sure that you have the latest version (1.6.0 as of this writing), as older ones read timecodes incorrectly.

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)

muxing:

Now mux to MKV:

  1. Open mmg.exe (mkvmerge gui)
  2. Add your video stream file
  3. Add your audio stream file
  4. Click on the imported video track
  5. Browse for the "timecodes.txt" timecode file
  6. Click on the audio track
  7. If your audio already needs a delay, set one
  8. Start muxing

To play it you need a Matroska splitter. For AVC you will need Haali's Splitter, but for ASP you can use it or Gabest's Splitter.

encoding to vfr (mp4)

If you create a 120 fps avi with drop-frames, however, the mp4 muxed from it will remove them along with any n-vops the encoder creates, leaving vfr. A more laborous way is to encode multiple cfr avi files (some with 23.976 fps film and some with 29.97 fps video) and join them directly into one vfr mp4 file with mp4box and the -cat option.

A third, much easier, method is to encode using the MKV method and then processing the video with tc2mp4: more details on tc2mp4 can be found on the Doom9 forums.

summary of the methods

Summing up the advantages and disadvantages of the above mentioned methods. When encoding to 23.976 or 29.97 fps the clip will be cfr (which editors like AviSynth and Virtualdub need), but it may look jumpy on playback due to duplicated or missing frames. That can be avoided with blending, but encoders can't work as well with that. When encoding to 120 fps using drop frames, the clip is cfr, not jumpy on playback, and very compatible for editing. Encoding to mkv using true vfr (using timecodes) neither loses nor duplicates frames, however it is not nearly as broadly supported as AVI.

Opening non MPEG-2 hybrid video in AviSynth and re-encoding

It is possible to open vfr video in AviSynth without losing sync: DirectShowSource. The most common formats that support hybrid video (vfr) are mkv, mp4, wmv, and rmvb, and the methods below work for all of them; however, if the source is mkv, you can also use mkv2vfr and AviSource.

opening non-avi vfr content in AviSynth

The best way to get all frames while keeping sync and timing is to convert to a common framerate, such as 120 fps for 24/30 (or rather 119.88). Always use convertfps=true, which adds frames like ChangeFPS, or your audio will go out of sync.

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

You can also open it as 30p, which then has to be re-decimated but has less frames to deal with, or 24p, breaking any 30p sections:

Re-encoding to 23.976 or 29.97 fps:

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

or

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

Another way is to find out the average framerate (by dividing the total number of frames by the duration in seconds) and use this rate in DirectShowSource. Depending on the duration of a frame, frames will be added or dropped to keep sync, and it's almost guaranteed to stutter. DirectShowSource will not telecine.

re-encoding 120 fps video

The easiest way to convert vfr sources back into vfr in AviSynth is by using 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 can also do this:

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")

Once you've encoded your file, mux back to mkv or 120 fps avi.

This will chop out all the duplicate frames directshowsource inserts, while keeping framecount and timing nearly identical. But do not use the timecode file from the input video, use the new one. They may not be identical. (Of course you can play with parameters if you want to use more of the functionality of dedup.)

converting vfr to cfr avi for AviSynth

You can avoid analysing and decimating by using special tools to get a minimal constant-rate avi to feed avisynth. After processing and re-encoding, use tc2cfr or mmg on the output with the original timecodes to regain vfr and full sync. (If you perform any kind of decimation or frame-rate change you'll have to edit the timecode file yourself, although dedup does have a timesin parameter.)

avi

avi_tc will create a timecode and normal video, if the avi uses drop frames and not n-vops or fully encoded frames. It also requires that no audio or secondary tracks are present. To use it, open tc-gui and add your file, or use the following command line:

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

mkv

mkv2vfr extracts all video frames from Matroska to a normal AVI file and a timecode file. This will only work if the mkv is in vfw-mode. The command-line to use it is:

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