C# 调用FFmpeg处理音视频的示例

FFmpeg 开源、跨平台、体积小、功能强大,提供了录制、转换以及流化音视频的完整解决方案。

官网:https://www.ffmpeg.org/

百科:https://baike.baidu.com/item/ffmpeg/2665727?fr=aladdin

FFmpeg 应用非常广泛,可以用来播放本地视频甚至网络视频,查看音视频信息,还可以用于从视频中提取音频,转换音视频文件格式等等,本文主要介绍如何调用 FFmpeg 来查看音视频信息、从视频中提取音频、转换音视频格式等。

  1. 调用FFmpeg
    调用原理相当于打开控制台输入命令,只不过在程序中将命令参数进行组合封装,以下是启动代码:
/// 
    /// 初始化命令、参数,启动线程
    /// 
    /// 
    /// 
    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();
    }

运行界面:

C# 调用FFmpeg处理音视频的示例_第1张图片

  1. 查看音视频信息
    命令如下(注: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%
  1. 软件包下载(以上调用过程和参数已经封装处理,软件目的c#教程是调用百度音频识别API将视频中的音频转化为文字)
    软件开源代码地址(项目集成文字识别、语音合成、语音识别):https://gitee.com/bidev_admin/river-int-ai.git

运行软件包下载地址(.Net Framework 4.5): https://pan.baidu.com/s/1z-WFDGHhEP8tqLqwp9I0Mw 提取码: r3sk

作者:沐汐 Vicky
出处:http://www.cnblogs.com/EasyInvoice

以上就是C# 调用FFmpeg处理音视频的示例的详细内容

你可能感兴趣的:(C#教程,c#教程)