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;

                }

            }

        }

    }

  • 相关阅读:
    又是七月的尾巴
    在Macbook Air上安装Archlinux
    又是一年毕业季
    orgpage a static site generator for emacs and org mode
    看完了《三体》三部曲
    暴强的命令行git提交历史记录查询
    xaml的window的AllowsTransparency属性在winxp下好像有bug,不知还有谁遇到过
    Swift 计算字符串展示的区域
    SwiftUI Stack中的View被压缩的效果
    如果git pull拉取分支出错,如何返回
  • 原文地址:https://www.cnblogs.com/luo-super/p/4722457.html
Copyright © 2011-2022 走看看