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

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

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

 

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

运行界面:

C# 调用 FFmpeg 处理音视频

 

 

 

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

相关文章: