本人项目中做到前端的一些视频页面开发,遇到有很多坑,主要问题在于浏览器支持的视频编码格式的问题上。很多视频没经过处理直接上传到后台,因为目前浏览器对视频编码格式的支持有限,导致很多视频都不能播放,特别是移动端IOS,兼容性很差。
[转]HTML5的视频格式之争
我这里想到的一种解决方案是上传视频到后台后进行转码处理, 处理成传统浏览器都支持的视频编码格式,比如H.264.
这里用的是C#控制台做的一个转码处理例子:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace VideoTranscoding
{
class Program
{
static void Main(string[] args)
{
Test1();
}
static string path = AppDomain.CurrentDomain.BaseDirectory;
static string pathVedio = "";
//原方法转码
private static void Test1()
{
Process p = new Process();
p.StartInfo.FileName = path + "ffmpeg.exe";
p.StartInfo.UseShellExecute = false;
string srcFileName = "";
string destFileName = "";
srcFileName = path + "1.WMV";
destFileName = path + "1.WMV";
//p.StartInfo.Arguments = "-i " + srcFileName + " -y -vcodec h264 -b 5942 " + destFileName; //执行参数
//p.StartInfo.Arguments = "-i " + srcFileName + " -c:v libx264 -crf 23 -c:a libfaac -q:a 100 output.mp4"; //执行参数
p.StartInfo.Arguments = "-i " + srcFileName + " -c:v libx264 -strict -2 output.mp4"; //执行参数
p.StartInfo.UseShellExecute = false; 不使用系统外壳程序启动进程
p.StartInfo.CreateNoWindow = true; //不显示dos程序窗口
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.StartInfo.UseShellExecute = false;
p.Start();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
}
private static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
private static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
}
}
例子中用的是WMV编码格式转MP4,具体其他转码方式,可自行百度或到ffmpeg官网查看学习。
源码链接: https://pan.baidu.com/s/12QBAiizdEYDnJserVUt61A 提取码: c4dg
[转]Ffmpeg常用转码命令
ffmpeg官网:http://ffmpeg.org