C# 调用FFmpeg处理音视频的示例
程序员文章站
2022-06-24 22:31:09
ffmpeg开源、跨平台、体积小、功能强大,提供了录制、转换以及流化音视频的完整解决方案。官网:百科:ffmpeg应用非常广泛,可以用来播放本地视频甚至网络视频,查看音视频信息,还可以用于从视频中提取...
ffmpeg 开源、跨平台、体积小、功能强大,提供了录制、转换以及流化音视频的完整解决方案。
官网:
百科:
ffmpeg 应用非常广泛,可以用来播放本地视频甚至网络视频,查看音视频信息,还可以用于从视频中提取音频,转换音视频文件格式等等,本文主要介绍如何调用 ffmpeg 来查看音视频信息、从视频中提取音频、转换音视频格式等。
1. 调用ffmpeg
调用原理相当于打开控制台输入命令,只不过在程序中将命令参数进行组合封装,以下是启动代码:
/// <summary> /// 初始化命令、参数,启动线程 /// </summary> /// <param name="args"></param> /// <param name="finishievent"></param> public static void start(string args, eventhandler finishievent) { loghelper.loginfoformat("开始执行命令, 参数:{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(); }
运行界面:
2. 查看音视频信息
命令如下(注:pcm文件不适用以下命令):
命令:ffprobe -print_format json -loglevel quiet -show_streams "您的文件" 示例:ffprobe -print_format json -loglevel quiet -show_streams "f:\mp4\3.tf以及地图构建.mp4"
以下是输出的文件格式等信息:
{ "streams": [ { "index": 0, "codec_name": "h264", "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 输入信息,如果一个文件中有多个音频或视频,将以数组形式输出,常用参数有: codec_name 表示编码, codec_type 类型, sample_rate 采样率, channels 声道, duration 时长(秒)
3.从视频中提取音频文件
命令:ffmpeg -i "您的视频文件路径" -vn -ar 采样率 "音频文件输出路径" 示例:ffmpeg -i "f:\mp4\3.tf以及地图构建.mp4" -vn -ar 8000 "f:\mp4\3.tf以及地图构建_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: h264 (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
4.转换音频格式:
命令:ffmpeg -i "源音频文件" -acodec pcm_s16le -f s16le -ar 目标音频采样率 "目标音频路径" 示例:ffmpeg -i "f:\mp4\3.tf以及地图构建_201215172919_8k.m4a" -acodec pcm_s16le -f s16le -ar 8000 "f:\mp4\3.tf以及地图构建_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%
5. 软件包下载(以上调用过程和参数已经封装处理,软件目的是调用百度音频识别api将视频中的音频转化为文字)
软件开源代码地址(项目集成文字识别、语音合成、语音识别):
运行软件包下载地址(.net framework 4.5): https://pan.baidu.com/s/1z-wfdghhep8tqlqwp9i0mw 提取码: r3sk
作者:沐汐 vicky
出处:http://www.cnblogs.com/easyinvoice
以上就是c# 调用ffmpeg处理音视频的示例的详细内容,更多关于c# 处理音视频的资料请关注其它相关文章!