2013年3月22日 星期五

ffmpeg 使用心得

ffmpeg 使用心得

為了要將影片轉成 ipod touch 3代,可用的格式,特別學習 ffmpeg,試了好幾天,終於找出來滿意的方法,特此記錄下來,免得忘了。

轉檔時的指令

ffmpeg -i $1 -s 720x406 -vpre ipod -ac 2 -sn -y -vf subtitles=$1.srt $1.m4v

其中

-i $1 是要轉檔的檔名

-s 720x406 是轉檔後的解析度,406 = 720 * 720 / 1280,(原始檔的解析度為 1280x720)。這個一定要去計算,不然,在電視上看,畫面比例會不對,字幕可能出不來。

-vpre ipod 是為了 ipod 特別找出來的 preset,設定如後。

--ac 2 是轉成立體聲,不然 ipod touch 沒辦法播放。

--sn 不顯示原始檔中的字幕。因為,我要用另外的字幕檔。

--vf subtitles 為字幕檔。

preset設定

preset 可以放在 ~/.ffmpeg,檔名為 arg.ffpreset,例如,我設定了一個專門轉到 ipod touch 的 preset 檔,命名為 ipod.ffpreset。

strict=-2

vcodec=libx264
vprofile=baseline
level=30
maxrate=10000000
bufsize=10000000

#preset=fast
preset=slower

crf=19
bf=0
refs=1

ab=192k

調整字幕時間

這個很少用到,在 linux 中,就找到 aegisub 是可用的,那就拿來用了,沒有什麼特別的考慮。

提取 mkv 內嵌字幕

為了要把 mkv 中的字幕,轉成繁體。使用以下三道指令即可:

ffmpeg -i $1.mkv -vn -an -scodec copy $1.ssa
opencc -i $1.ssa -o o.ssa
ffmpeg -i $1 -s 720x406 -vpre ipod -ac 2 -sn -y -vf ass=$1.ssa $1.m4v

參考資料

ffmpeg help

Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Getting help:
    -h      -- print basic options
    -h long -- print more options
    -h full -- print all options (including all format and codec specific options, very long)
    See man ffmpeg for detailed description of the options.

Print help / information / capabilities:
-L                  show license
-h topic            show help
-? topic            show help
-help topic         show help
--help topic        show help
-version            show version
-formats            show available formats
-codecs             show available codecs
-decoders           show available decoders
-encoders           show available encoders
-bsfs               show available bit stream filters
-protocols          show available protocols
-filters            show available filters
-pix_fmts           show available pixel formats
-layouts            show standard channel layouts
-sample_fmts        show available audio sample formats

Global options (affect whole program instead of just one file:
-loglevel loglevel  set libav* logging level
-v loglevel         set libav* logging level
-report             generate a report
-max_alloc bytes    set maximum size of a single allocated block
-y                  overwrite output files
-n                  do not overwrite output files
-stats              print progress report during encoding
-bits_per_raw_sample number  set the number of bits per raw sample
-croptop size       Removed, use the crop filter instead
-cropbottom size    Removed, use the crop filter instead
-cropleft size      Removed, use the crop filter instead
-cropright size     Removed, use the crop filter instead
-padtop size        Removed, use the pad filter instead
-padbottom size     Removed, use the pad filter instead
-padleft size       Removed, use the pad filter instead
-padright size      Removed, use the pad filter instead
-padcolor color     Removed, use the pad filter instead
-vol volume         change audio volume (256=normal)

Per-file main options:
-f fmt              force format
-c codec            codec name
-codec codec        codec name
-pre preset         preset name
-map_metadata outfile[,metadata]:infile[,metadata]  set metadata information of outfile from infile
-t duration         record or transcode "duration" seconds of audio/video
-fs limit_size      set the limit file size in bytes
-ss time_off        set the start time offset
-timestamp time     set the recording timestamp ('now' to set the current time)
-metadata string=string  add metadata
-target type        specify target file type ("vcd", "svcd", "dvd", "dv", "dv50", "pal-vcd", "ntsc-svcd", ...)
-frames number      set the number of frames to record
-filter filter_list  set stream filterchain
-reinit_filter      reinit filtergraph on input parameter changes

Video options:
-vframes number     set the number of video frames to record
-r rate             set frame rate (Hz value, fraction or abbreviation)
-s size             set frame size (WxH or abbreviation)
-aspect aspect      set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number  set the number of bits per raw sample
-croptop size       Removed, use the crop filter instead
-cropbottom size    Removed, use the crop filter instead
-cropleft size      Removed, use the crop filter instead
-cropright size     Removed, use the crop filter instead
-padtop size        Removed, use the pad filter instead
-padbottom size     Removed, use the pad filter instead
-padleft size       Removed, use the pad filter instead
-padright size      Removed, use the pad filter instead
-padcolor color     Removed, use the pad filter instead
-vn                 disable video
-vcodec codec       force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff  set initial TimeCode value.
-pass n             select the pass number (1 to 3)
-vf filter list     video filters
-b bitrate          video bitrate (please use -b:v)
-dn                 disable data

Audio options:
-aframes number     set the number of audio frames to record
-aq quality         set audio quality (codec-specific)
-ar rate            set audio sampling rate (in Hz)
-ac channels        set number of audio channels
-an                 disable audio
-acodec codec       force audio codec ('copy' to copy stream)
-vol volume         change audio volume (256=normal)
-af filter list     audio filters

Subtitle options:
-s size             set frame size (WxH or abbreviation)
-sn                 disable subtitle
-scodec codec       force subtitle codec ('copy' to copy stream)
-stag fourcc/tag    force subtitle tag/fourcc
-fix_sub_duration   fix subtitles duration
-spre preset        set the subtitle options to the indicated preset

參考指令

假設video.avi有一個視頻軌、兩個音頻軌,取其中的視頻軌和第二個音頻軌加sub.ass做成video.mkv而不重新編碼:

ffmpeg -i video.avi -i sub.ass -map 0:0 -map 0:2 -map 1 -c:a copy -c:v copy -c:s copy video.mkv

如果要重新編碼(字幕合成到視頻圖像):

ffmpeg -i video.avi -map 0:0 -map 0:2 -c:a copy -c:v <視頻編碼庫,比如libx264> -vf ass=sub.ass video.mkv