您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)如何在C#項目中使用FFmpeg對音視頻進行處理,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
FFmpeg 開源、跨平臺、體積小、功能強大,提供了錄制、轉(zhuǎn)換以及流化音視頻的完整解決方案。
FFmpeg 應(yīng)用非常廣泛,可以用來播放本地視頻甚至網(wǎng)絡(luò)視頻,查看音視頻信息,還可以用于從視頻中提取音頻,轉(zhuǎn)換音視頻文件格式等等,本文主要介紹如何調(diào)用 FFmpeg 來查看音視頻信息、從視頻中提取音頻、轉(zhuǎn)換音視頻格式等。
調(diào)用原理相當于打開控制臺輸入命令,只不過在程序中將命令參數(shù)進行組合封裝,以下是啟動代碼:
/// <summary> /// 初始化命令、參數(shù),啟動線程 /// </summary> /// <param name="args"></param> /// <param name="finishiEvent"></param> public static void Start(string args, EventHandler finishiEvent) { LogHelper.LogInfoFormat("開始執(zhí)行命令, 參數(shù):{0}", args); FinishiEvent = finishiEvent; // ffmpeg.exe組件文件路徑 string ffmpegPath = FFmpegBinariesHelper.RegisterFFmpegBinaries(); string cmdType = args.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)[0]; ProcessStartInfo startInfo = new ProcessStartInfo(Path.Combine(ffmpegPath, cmdType)); startInfo.WindowStyle = ProcessWindowStyle.Normal; startInfo.Arguments = args.Substring(cmdType.Length); startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true;// ffmpeg信息輸出是error事件 p.StartInfo = startInfo; p.OutputDataReceived += P_OutputDataReceived; // ffmpeg信息輸出事件 p.ErrorDataReceived += P_OutputDataReceived; Thread thread = new Thread(startffmpeg); thread.Start(); }
運行界面:
命令如下(注:pcm文件不適用以下命令):
命令:ffprobe -print_format json -loglevel quiet -show_streams "您的文件" 示例:ffprobe -print_format json -loglevel quiet -show_streams "F:\mp4\3.TF以及地圖構(gòu)建.mp4"
以下是輸出的文件格式等信息:
{ "streams": [ { "index": 0, "codec_name": "h364", "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10", "profile": "High", "codec_type": "video", "codec_time_base": "1046271/31388000", "codec_tag_string": "avc1", "codec_tag": "0x31637661", "width": 960, "height": 640, "coded_width": 960, "coded_height": 640, "has_b_frames": 0, "sample_aspect_ratio": "1:1", "display_aspect_ratio": "3:2", "pix_fmt": "yuv420p", "level": 31, "chroma_location": "left", "refs": 1, "is_avc": "true", "nal_length_size": "4", "r_frame_rate": "15/1", "avg_frame_rate": "15694000/1046271", "time_base": "1/90000", "start_pts": 0, "start_time": "0.000000", "duration_ts": 188328780, "duration": "2092.542000", "bit_rate": "358855", "bits_per_raw_sample": "8", "nb_frames": "31388", "disposition": { "default": 1, "dub": 0, "original": 0, "comment": 0, "lyrics": 0, "karaoke": 0, "forced": 0, "hearing_impaired": 0, "visual_impaired": 0, "clean_effects": 0, "attached_pic": 0, "timed_thumbnails": 0 }, "tags": { "creation_time": "2020-09-04T01:03:03.000000Z", "language": "und", "encoder": "JVT/AVC Coding" } }, { "index": 1, "codec_name": "aac", "codec_long_name": "AAC (Advanced Audio Coding)", "profile": "LC", "codec_type": "audio", "codec_time_base": "1/48000", "codec_tag_string": "mp4a", "codec_tag": "0x6134706d", "sample_fmt": "fltp", "sample_rate": "48000", "channels": 1, "channel_layout": "mono", "bits_per_sample": 0, "r_frame_rate": "0/0", "avg_frame_rate": "0/0", "time_base": "1/48000", "start_pts": 0, "start_time": "0.000000", "duration_ts": 100440014, "duration": "2092.500292", "bit_rate": "64000", "max_bit_rate": "66584", "nb_frames": "98086", "disposition": { "default": 1, "dub": 0, "original": 0, "comment": 0, "lyrics": 0, "karaoke": 0, "forced": 0, "hearing_impaired": 0, "visual_impaired": 0, "clean_effects": 0, "attached_pic": 0, "timed_thumbnails": 0 }, "tags": { "creation_time": "2020-09-04T01:03:03.000000Z", "language": "und" } } ] }
streams 輸入信息,如果一個文件中有多個音頻或視頻,將以數(shù)組形式輸出,常用參數(shù)有: codec_name 表示編碼, codec_type 類型, sample_rate 采樣率, channels 聲道, duration 時長(秒)
命令:ffmpeg -i "您的視頻文件路徑" -vn -ar 采樣率 "音頻文件輸出路徑" 示例:ffmpeg -i "F:\mp4\3.TF以及地圖構(gòu)建.mp4" -vn -ar 8000 "F:\mp4\3.TF以及地圖構(gòu)建_201215172919_8k.m4a"
以下是處理過程輸出信息:
ffmpeg version git-2020-04-17-889ad93 Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9.3.1 (GCC) 20200328 configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libsrt --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --disable-w32threads --enable-libmfx --enable-ffnvcodec --enable-cuda-llvm --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf libavutil 56. 42.102 / 56. 42.102 libavcodec 58. 80.100 / 58. 80.100 libavformat 58. 42.100 / 58. 42.100 libavdevice 58. 9.103 / 58. 9.103 libavfilter 7. 77.101 / 7. 77.101 libswscale 5. 6.101 / 5. 6.101 libswresample 3. 6.100 / 3. 6.100 libpostproc 55. 6.100 / 55. 6.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'F:\mp4\3.TF浠ュ強鍦板浘鏋勫緩.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42isom creation_time : 2020-09-04T01:03:03.000000Z Duration: 00:34:52.54, start: 0.000000, bitrate: 425 kb/s Stream #0:0(und): Video: h364 (High) (avc1 / 0x31637661), yuv420p, 960x640 [SAR 1:1 DAR 3:2], 358 kb/s, 15 fps, 15 tbr, 90k tbn, 30 tbc (default) Metadata: creation_time : 2020-09-04T01:03:03.000000Z encoder : JVT/AVC Coding Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 64 kb/s (default) Metadata: creation_time : 2020-09-04T01:03:03.000000Z Stream mapping: Stream #0:1 -> #0:0 (aac (native) -> aac (native)) Press [q] to stop, [?] for help [aac @ 042107c0] Too many bits 8832.000000 > 6144 per frame requested, clamping to max Output #0, ipod, to 'F:\mp4\3.TF浠ュ強鍦板浘鏋勫緩_201215172919_8k.m4a': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42isom encoder : Lavf58.42.100 Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 8000 Hz, mono, fltp, 48 kb/s (default) Metadata: creation_time : 2020-09-04T01:03:03.000000Z encoder : Lavc58.80.100 aac size= 256kB time=00:01:42.40 bitrate= 20.5kbits/s speed= 204x size= 512kB time=00:03:28.64 bitrate= 20.1kbits/s speed= 208x size= 768kB time=00:05:12.19 bitrate= 20.2kbits/s speed= 208x size= 1280kB time=00:06:59.84 bitrate= 25.0kbits/s speed= 210x size= 1536kB time=00:08:38.52 bitrate= 24.3kbits/s speed= 207x size= 1792kB time=00:10:20.41 bitrate= 23.7kbits/s speed= 206x size= 2304kB time=00:12:11.77 bitrate= 25.8kbits/s speed= 209x size= 2560kB time=00:13:55.32 bitrate= 25.1kbits/s speed= 209x size= 2816kB time=00:15:40.41 bitrate= 24.5kbits/s speed= 209x size= 3072kB time=00:17:30.36 bitrate= 24.0kbits/s speed= 210x size= 3584kB time=00:19:17.12 bitrate= 25.4kbits/s speed= 210x size= 3840kB time=00:20:57.85 bitrate= 25.0kbits/s speed= 209x size= 4096kB time=00:22:44.73 bitrate= 24.6kbits/s speed= 210x size= 4608kB time=00:24:15.61 bitrate= 25.9kbits/s speed= 208x size= 4864kB time=00:25:52.63 bitrate= 25.7kbits/s speed= 207x size= 5120kB time=00:27:26.20 bitrate= 25.5kbits/s speed= 205x size= 5376kB time=00:29:04.38 bitrate= 25.2kbits/s speed= 205x size= 5632kB time=00:30:40.12 bitrate= 25.1kbits/s speed= 204x size= 6144kB time=00:32:20.47 bitrate= 25.9kbits/s speed= 204x size= 6400kB time=00:34:38.07 bitrate= 25.2kbits/s speed= 207x size= 6558kB time=00:34:52.54 bitrate= 25.7kbits/s speed= 209x video:0kB audio:6493kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.997352% [aac @ 042107c0] Qavg: 65535.836
命令:ffmpeg -i "源音頻文件" -acodec pcm_s16le -f s16le -ar 目標音頻采樣率 "目標音頻路徑" 示例:ffmpeg -i "F:\mp4\3.TF以及地圖構(gòu)建_201215172919_8k.m4a" -acodec pcm_s16le -f s16le -ar 8000 "F:\mp4\3.TF以及地圖構(gòu)建_201215172919_8k_201215173520_8k.pcm"
處理過程輸出信息:
ffmpeg version git-2020-04-17-889ad93 Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9.3.1 (GCC) 20200328 configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libsrt --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --disable-w32threads --enable-libmfx --enable-ffnvcodec --enable-cuda-llvm --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf libavutil 56. 42.102 / 56. 42.102 libavcodec 58. 80.100 / 58. 80.100 libavformat 58. 42.100 / 58. 42.100 libavdevice 58. 9.103 / 58. 9.103 libavfilter 7. 77.101 / 7. 77.101 libswscale 5. 6.101 / 5. 6.101 libswresample 3. 6.100 / 3. 6.100 libpostproc 55. 6.100 / 55. 6.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'F:\mp4\3.TF浠ュ強鍦板浘鏋勫緩_201215172919_8k.m4a': Metadata: major_brand : M4A minor_version : 512 compatible_brands: M4A isomiso2 encoder : Lavf58.42.100 Duration: 00:34:52.63, start: 0.000000, bitrate: 25 kb/s Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 8000 Hz, mono, fltp, 25 kb/s (default) Metadata: handler_name : SoundHandler Stream mapping: Stream #0:0 -> #0:0 (aac (native) -> pcm_s16le (native)) Press [q] to stop, [?] for help Output #0, s16le, to 'F:\mp4\3.TF浠ュ強鍦板浘鏋勫緩_201215172919_8k_201215173520_8k.pcm': Metadata: major_brand : M4A minor_version : 512 compatible_brands: M4A isomiso2 encoder : Lavf58.42.100 Stream #0:0(und): Audio: pcm_s16le, 8000 Hz, mono, s16, 128 kb/s (default) Metadata: handler_name : SoundHandler encoder : Lavc58.80.100 pcm_s16le size= 32696kB time=00:34:52.54 bitrate= 128.0kbits/s speed=8.63e+03x video:0kB audio:32696kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
以上就是如何在C#項目中使用FFmpeg對音視頻進行處理,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。