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();
    }

运行界面:

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"
      }
    }
  ]
}

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将视频中的音频转化为文字)

软件开源代码地址(项目集成文字识别、语音合成、语音识别):https://gitee.com/bidev_admin/river-int-ai.git

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

以上就是C# 调用FFmpeg处理音视频的示例的详细内容,更多关于c# 处理音视频的资料请关注乐虎体育其它相关文章!

C# 调用FFmpeg处理音视频的示例的更多相关文章

  1. C#实现简单的五子棋游戏

    最近利用业余时间写了一个简单的五子棋游戏,没有利用深层次的面向对象技术,自学一年,代码和程序设计有不妥之处,还望大神......

  2. 浅谈ASP.NET Core中IOC与DI的理解和使用

    说起IOC和DI,使用过ASP.NET Core的人对这两个概念一定不陌生,早前,自己也有尝试过去了解这两个东西,但......

  3. 巧用 Lazy 解决.NET Core中的循环依赖问题

    原文作者: Thomas Levesque原文链接:https://thomaslevesque.com/2020/......

  4. 浅入 .NET Core 中的内存和GC知识

    目录 托管代码 自动内存管理 参考资料: 【1】https://docs.microsoft.com/z......

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

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

  6. asp.net core集成CKEditor实现图片上传功能的示例代码

    背景本文为大家分享了asp.net core 如何集成CKEditor ,并实现图片上传功能的具体方法,供大家参考,......

随机推荐

  1. C++ 入门篇

    C++基础入门 1 C++初识 1.1 第一个C++程序 编写一个C++程序总共分为4个步骤 创建项目 ......

  2. python基于opencv实现人脸识别

    将opencv中haarcascade_frontalface_default.xml文件下载到本地,我们调用它辅助......

  3. PHP设计模式之命令模式示例详解

    前言命令模式,也称为动作或者事务模式,很多教材会用饭馆来举例。作为顾客的我们是命令的下达者,服务员是这个命令的接收者......

  4. Java中while语句的简单知识及应用

    先谈谈while循环的三要素while循环的三要素:(1)初始化变量(2)循环条件(3)改变循环变量的值当你要用wh......

  5. java中throws实例用法详解

    在程序出现异常时,会有一个抛出异常的throw出现,这里我们要跟今天所讲的throws区分开。throws的作用是声......

  6. 手把手教你搭建SSH框架(Eclipse版)

    原文来自公众号【C you again】,若需下载完整源码,请在公众号后台回复“ssh”。本期文章详细讲解了SSH(......

  7. JavaScript canvas实现文字时钟

    本文实例为大家分享了canvas实现文字时钟的具体代码,供大家参考,具体内容如下 先看看效果图 代码 &l......

  8. SpringCloud | 通过电商业务场景让你彻底明白SpringCloud核心组件的底层原理

    本文分为两个部分:Spring Cloud"全家桶"简单介绍。通过实际电商业务场景,让你彻底明白......

  9. node.js常用内置模块一

    在使用内模块的时候需要先将所需的内置模块进行引入、OS模块在nodejs中OS模块提供了与操作系统相关的属性和方法/......

  10. 浅入 .NET Core 中的内存和GC知识

    目录 托管代码 自动内存管理 参考资料: 【1】https://docs.microsoft.com/z......