0 什麼是ffmpeg?
Libav(舊稱:FFmpeg)是一個自由軟體,可以運行音頻和視頻多種格式的錄影、轉檔、流功能[1],包含了libavcodec
─這是一個用於多個專案中音頻和視頻的解碼器庫,以及 libavformat ——一個音頻與視頻格式轉換庫。
libav的舊稱"FFmpeg"這個單詞中的 "FF" 指的是 "Fast Forward"[2]。有些新手寫信給"FFmpeg"的項目負責人,詢問FF是不是代表“Fast
Free”或者“Fast Fourier”等意思,"FFmpeg"的項目負責人回信說“Just for the record, the original meaning of "FF" in FFmpeg is "Fast Forward"...”
http://zh.wikipedia.org/wiki/Libav
一 下載ffmpeg
官方網站: http://ffmpeg.zeranoe.com/
訪問 http://ffmpeg.zeranoe.com/builds/
下載ffmpeg windows下發布版
FFmpeg
git-f514695 32-bit Shared (Latest)
二 在命令列下學會ffmpeg使用
cd 進入ffmpeg解壓縮目錄下的bin目錄
ffmpeg.exe -i D:/source.avi -vcodec copy -y -r 25 -ss 8 -t 971 D:/demo.mp4 >> D:\clip_info.txt 2>&1
這裡-vcodec copy參數一定要加,這樣裁剪的時候不會改變視頻的編碼方式,裁剪非常快。
ffmpeg主要參數一覽
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
Main options:
-L show license
-h show help
-? show help
-help show help
--help show help
-version show version
-formats show available formats
-codecs show available codecs
-bsfs show available bit stream filters
-protocols show available protocols
-filters show available filters
-pix_fmts show available pixel formats
-sample_fmts show available audio sample formats
-loglevel loglevel set libav* logging level
-v loglevel set libav* logging level
-debug flags set debug flags
-report generate a report
-max_alloc bytes set maximum size of a single allocated block
-f fmt force format
-i filename input file name
-y overwrite output files
-n do not overwrite output files
-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
-itsoffset time_off set the input ts offset
-itsscale scale set the input ts scale
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string add metadata
-dframes number set the number of data frames to record
-timelimit limit set max runtime in seconds
-target type specify target file type ("vcd", "svcd", "dvd", "dv", "dv50", "pal-vcd", "ntsc-svcd", ...)
-xerror exit on error
-frames number set the number of frames to record
-tag fourcc/tag force codec tag/fourcc
-filter filter_list set stream filterchain
-stats print progress report during encoding
-attach filename add an attachment to the output file
-dump_attachment filename extract an attachment into a file
-bsf bitstream_filters A comma-separated list of bitstream filters
-dcodec codec force data codec ('copy' to copy stream)
Advanced options:
-map [-]input_file_id[:stream_specifier][,sync_file_id[:stream_s set input stream mapping
-map_channel file.stream.channel[:syncfile.syncstream] map an audio channel from one stream to another
-map_chapters input_file_index set chapters mapping
-benchmark add timings for benchmarking
-dump dump each input packet
-hex when dumping packets, also dump the payload
-re read input at native frame rate
-loop_input deprecated, use -loop
-loop_output deprecated, use -loop
-vsync video sync method
-async audio sync method
-adrift_threshold threshold audio drift threshold
-copyts copy timestamps
-copytb source copy input stream time base when stream copying
-shortest finish encoding within shortest input
-dts_delta_threshold threshold timestamp discontinuity delta threshold
-copyinkf copy initial non-keyframes
-q q use fixed quality scale (VBR)
-qscale q use fixed quality scale (VBR)
-streamid streamIndex:value set the value of an outfile streamid
-muxdelay seconds set the maximum demux-decode delay
-muxpreload seconds set the initial demux-decode delay
-fpre filename set options from indicated preset file
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)
-sameq use same quantizer as source (implies VBR)
-same_quant use same quantizer as source (implies VBR)
-pass n select the pass number (1 or 2)
-passlogfile prefix select two pass log file name prefix
-vf filter list video filters
-b bitrate video bitrate (please use -b:v)
-dn disable data
三 利用QT4做個簡單圖形介面工具
這裡主要用了QProcess這個類,可以調用外部程式。
關鍵代碼:
QString program = "D:\\maxview_video_demo\\ffmpeg\\ffmpeg-git-985e768-win64-static\\bin\\ffmpeg.exe";
QString inputPath = ui->videopathLineEdit->text();
QFile sourceFile(inputPath);
if(!sourceFile.exists()){
QMessageBox::information(this,QString::fromUtf8("提示"),QString::fromUtf8("找不到源檔案"));
return;
}
QString outputPath = QFileInfo(sourceFile).absolutePath() +"/clip.mp4";
QFile destFile(outputPath);
if(destFile.exists()){
destFile.remove();
}
QString startTime = ui->videoStartTimeEdit->time().toString("hh:mm:ss");
QString len= ui->videoLengthTimeEdit->time().toString("hh:mm:ss");
QStringList arguments;
arguments << "-i" << inputPath << "-r" << "25"<<"-ss";
arguments <<startTime<< "-t" << len<< outputPath;
QProcess *clipProcess = new QProcess(this);
connect(clipProcess,SIGNAL(finished(int)),this,SLOT(clipVideoFinished(int)));
clipProcess->start(program, arguments);
當視頻截取完成後,進程會發生訊號finished(int),並被QProcess捕捉到,這樣可以調用
clipVideoFinished函數來通知程式視頻裁剪完成了。