zoukankan      html  css  js  c++  java
  • 视频转码在实际项目中的应用

    视频转码在实际项目中的应用

    前言:因之前有遇到在项目中将.flv文件视频转换为.mp4,故作此记录,以下是使用ffmpeg.exe作为转码工具。

    1:接下来需要准备工具;http://ffmpeg.org/  下载相应版本的ffmpeg.exe(64+32)

    2:实现代码,以下只是其中一种实现方式:

    using System;

    using System.Collections.Generic;

    using System.Diagnostics;

    using System.Linq;

    using System.Web;

    namespace VideoConvert.Models

    {

        public class FFmpegHelper

        {

            /// <summary>

            /// 32位

            /// </summary>

            private static string ffmpegPath32 = AppDomain.CurrentDomain.BaseDirectory + @"ffmpeg32ffmpeg.exe";

            /// <summary>

            /// 64位

            /// </summary>

            private static string ffmpegPath64 = AppDomain.CurrentDomain.BaseDirectory + @"ffmpeg64ffmpeg.exe";

            /// <summary>

            /// 实际版本

            /// </summary>

            private static string path;

            private static Process p = null;

            /// <summary>

            /// 将flv转码为 mp4

            /// </summary>

            /// <param name="oldPath">....flv</param>

            /// <param name="newPath">.....mp4</param>

            public void VideoConvert(string oldPath,string newPath)

            {

                using (p = new Process())

                {

                    //更多转码方式可以查看官方文档,以下只是其中一种:

                    string arg = "  -i  " + oldPath + "  -c:v libx264 -crf 23 -c:a libfaac -q:a 100 " + newPath;

                    //判断系统版本:

                    Is32Or64();

                    p.StartInfo.Arguments = arg;

                    p.StartInfo.FileName = path;

                    p.StartInfo.RedirectStandardError = true;

                    p.StartInfo.RedirectStandardInput = true;

                    p.StartInfo.RedirectStandardOutput = true;

                    //表示不显示转码窗口

                    p.StartInfo.CreateNoWindow = true;

                    p.StartInfo.UseShellExecute = false;

                    //设置进程终止时触发事件;

                    p.EnableRaisingEvents = true;

                    p.Exited += new EventHandler(p_Exited);

                    p.OutputDataReceived +=new DataReceivedEventHandler(p_OutputDataReceived);

                    p.ErrorDataReceived +=new DataReceivedEventHandler(p_ErrorDataReceived);

                    p.Start();

                    //读取输出;

                    p.BeginOutputReadLine();

                    p.BeginErrorReadLine();

                    //设置等待进程触发p_Exited事件后在往下执行;

                    p.WaitForExit();

                }

            }

            void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)

            {

                //记录输出日志

            }

            void p_OutputDataReceived(object sender, DataReceivedEventArgs e)

            {

                //记录输出日志

            }

            void p_Exited(object sender, EventArgs e)

            {

                //进程退出触发该事件,可以利用此执行其它操作或者是判断

            }

            public void Is32Or64()

            {

                if (Environment.Is64BitOperatingSystem)

                {

                    path = ffmpegPath64;

                }

                else {

                    path = ffmpegPath32;

                }

            }

        }

    }

  • 相关阅读:
    HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)
    SPOJ-DQUERY-D-query
    视频上云/网络穿透/拉转推工具EasyNTS设备IP地址返回数据与实际IP匹配的筛选机制优化
    视频上云/网络穿透/拉转推工具EasyNTS新增获取windows所有盘符信息功能的实现
    【解决方案】热门景区实现智慧旅游,城市道路/风景区视频公众号分享该如何实现?
    TSINGSEE青犀视频开发webrtc使用ffmpeg编译报ffmpeg version: N-94448-G3CEA9CD219提示是什么原因
    TSINGSEE青犀视频开发webrtc浏览器使用video标签播放webrtc本地录音音频实现过程
    【解决方案】智慧农业自动化的浪潮下,大棚实时视频监控系统应该如何搭建?
    【解决方案】房地产行业施工现场搭建视频4G无线远程视频监管信息化行业应用方案建议书
    【解决方案】国标GB28181协议视频智能分析平台EasyCVR搭建智慧养殖平台,让畜牧业实现“万物互联”
  • 原文地址:https://www.cnblogs.com/luo-super/p/4722457.html
Copyright © 2011-2022 走看看