본문 바로가기

IT/Linux

[Linux] ffmpeg 동영상 코덱 변경

ffmpeg -i '파일명.avi' -c:v libx264 -crf 19 -preset slow -c:a aac -b:a 192k -ac 2 파일명.mp4;

 

폴더에 있는 모든 avi 파일을 mp4로 바꾸는 script  

for FILE in *.avi;
ffmpeg -i $FILE -c:v libx264 -crf 19 -preset slow -c:a aac -b:a 192k -ac 2 ${FILE%.mp4};

 

Select codec, 코덱 설정

Input option으로 설정하면 encoder의 codec을 설정한다. Output option으로 설정하면 decoder의 codec을 설정한다.

 

Constant Rate Factor, 품질 조절

Bitrate대신 화질로 품질을 조절하는 방법이다. (0 ~ 63)의 값으로 낮을수록 품질이 높아지고 파일의 용량이 커진다. (0으로 설정하면 손실없는 가장 높은 품질의 결과를 얻을 수 있다.)

 

Set bit rate in bits/s, bitrate 조절

1초에 해당하는 동영상에 얼마의 bit를 넣느냐를 의미한다. 높을수록 더 많은 정보를 담아서 화질이 좋아지게 된다. 대표적으로 CBR, VBR 방식이 있으며, CBR(Constant Bitrate)은 고정된 bitrate를 VBR(Variable Bitrate)는 최대, 최소 범위로 변동하는 bitrate를 사용하는 방식을 의미한다. ffmpeg는 기본적으로 CBR 방식을 사용하며, 옵션(vbr)을 통해서 VBR 방식을 사용할 수 있다.

값을 설정하면 codec이 처리 가능한 가장 가까운 bitrate로 설정된다.

 

  • 옵션
Global options (affect whole program instead of just one file:
-loglevel loglevel  set logging level
-v loglevel         set logging level
-report             generate a report
-max_alloc bytes    set maximum size of a single allocated block
-y                  overwrite output files
-n                  never overwrite output files
-ignore_unknown     Ignore unknown stream types
-filter_threads     number of non-complex filter threads
-filter_complex_threads  number of threads for -filter_complex
-stats              print progress report during encoding
-max_error_rate maximum error rate  ratio of errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.
-bits_per_raw_sample number  set the number of bits per raw sample
-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
-to time_stop       record or transcode stop time
-fs limit_size      set the limit file size in bytes
-ss time_off        set the start time offset
-sseof time_off     set the start time offset relative to EOF
-seek_timestamp     enable/disable seeking by timestamp with -ss
-timestamp time     set the recording timestamp ('now' to set the current time)
-metadata string=string  add metadata
-program title=string:st=number...  add program with specified streams
-target type        specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad               audio pad
-frames number      set the number of frames to output
-filter filter_graph  set stream filtergraph
-filter_script filename  read stream filtergraph description from a file
-reinit_filter      reinit filtergraph on input parameter changes
-discard            discard
-disposition        disposition

Video options:
-vframes number     set the number of video frames to output
-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
-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_graph    set video filters
-ab bitrate         audio bitrate (please use -b:a)
-b bitrate          video bitrate (please use -b:v)
-dn                 disable data

Audio options:
-aframes number     set the number of audio frames to output
-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_graph    set 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
-canvas_size size   set canvas size (WxH or abbreviation)
-spre preset        set the subtitle options to the indicated preset