FFMPEG的强大无需多说,举几个用到的功能,直接贴代码了

还有更多命令用到时搜索即可

视频转码

public static string DecodeMp4ToFlv(string mp4, string format = ".flv", int timeout = )
{
var args = "-y -i {0} -vcodec copy {1}".Formatting("\"{0}\"".Formatting(mp4), "\"{0}\"".Formatting(strFlvPath));
string output, error;
if (timeout <= )
timeout = **; // 超时时间 = 5 分钟
ProcessHelper.Process(strFFMPEGPath, args, timeout, out output, out error);
if (!error.IsNullOrEmpty())
{
Logger.Error("{0}{1} : {2}{0}".Formatting(Environment.NewLine, "FFmpeg", error));
} return flv;
}

视频合并

public static string ConcatMp4(string mp41, string mp42)
{ var args = " -i \"concat:" + mp41 + "|" + mp42 + "|\" -c copy -bsf:a aac_adtstoasc -movflags +faststart " + outputpath;
string output, error; int timeout = * * ; // 超时时间 = 2 分钟
ProcessHelper.Process(strFFMPEGPath, args, timeout, out output, out error);
if (!error.IsNullOrEmpty())
{
Logger.Error("{0}{1} : {2}{0}".Formatting(Environment.NewLine, "FFmpeg", error));
} return outputpath;
 }

获取视频时长

private static int GetVideoDuration(string ffmpegfile, string sourceFile)
{
try
{
using (System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process())
{
String duration; // soon will hold our video's duration in the form "HH:MM:SS.UU"
String result; // temp variable holding a string representation of our video's duration
StreamReader errorreader; // StringWriter to hold output from ffmpeg // we want to execute the process without opening a shell
ffmpeg.StartInfo.UseShellExecute = false;
//ffmpeg.StartInfo.ErrorDialog = false;
ffmpeg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// redirect StandardError so we can parse it
// for some reason the output comes through over StandardError
ffmpeg.StartInfo.RedirectStandardError = true;
// set the file name of our process, including the full path
// (as well as quotes, as if you were calling it from the command-line)
ffmpeg.StartInfo.FileName = ffmpegfile; // set the command-line arguments of our process, including full paths of any files
// (as well as quotes, as if you were passing these arguments on the command-line)
ffmpeg.StartInfo.Arguments = "-i " + sourceFile; // start the process
ffmpeg.Start(); // now that the process is started, we can redirect output to the StreamReader we defined
errorreader = ffmpeg.StandardError; // wait until ffmpeg comes back
ffmpeg.WaitForExit(); // read the output from ffmpeg, which for some reason is found in Process.StandardError
result = errorreader.ReadToEnd(); // a little convoluded, this string manipulation...
// working from the inside out, it:
// takes a substring of result, starting from the end of the "Duration: " label contained within,
// (execute "ffmpeg.exe -i somevideofile" on the command-line to verify for yourself that it is there)
// and going the full length of the timestamp duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length); string[] ss = duration.Split(':');
int h = int.Parse(ss[]);
int m = int.Parse(ss[]);
int s = int.Parse(ss[]);
return h * + m * + s;
}
}
catch (System.Exception ex)
{
return ;
}
}
Process处理类如下,也可以自己写个
public static void Process(string startFile, string args, int timeout, out string standardOutput,
out string standardError)
{
using (var process = new ProcessExecutor(startFile, args, timeout))
{
process.Execute(out standardOutput, out standardError);
}
}
internal class ProcessExecutor : IDisposable
{
private readonly StringBuilder error;
private readonly AutoResetEvent errorWaitHandle;
private readonly StringBuilder output;
private readonly int timeout;
private AutoResetEvent outputWaitHandle;
private Process process; public ProcessExecutor(string startFile, string args, int timeout = )
{
process = new Process();
//设置进程启动信息属性StartInfo,这是ProcessStartInfo类
process.StartInfo.FileName = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(startFile));
process.StartInfo.Arguments = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(args)); process.StartInfo.UseShellExecute = false;
//提供的标准输出流只有2k,超过大小会卡住;如果有大量输出,就读出来
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true; output = new StringBuilder();
error = new StringBuilder(); outputWaitHandle = new AutoResetEvent(false);
errorWaitHandle = new AutoResetEvent(false); this.timeout = timeout; RegisterToEvents();
} public void Dispose()
{
UnregisterFromEvents(); if (process != null)
{
process.Dispose();
process = null;
}
if (errorWaitHandle != null)
{
errorWaitHandle.Close();
outputWaitHandle = null;
}
if (outputWaitHandle != null)
{
outputWaitHandle.Close();
outputWaitHandle = null;
}
} public void Execute(out string standardOutput, out string standardError)
{
process.Start(); process.BeginOutputReadLine();
process.BeginErrorReadLine(); if (process.WaitForExit(timeout) &&
outputWaitHandle.WaitOne(timeout) &&
errorWaitHandle.WaitOne(timeout))
{ }
else
{
// if timeout then kill the procee
process.Kill();
} standardOutput = output.ToString();
standardError = error.ToString();
} private void RegisterToEvents()
{
process.OutputDataReceived += process_OutputDataReceived;
process.ErrorDataReceived += process_ErrorDataReceived;
} private void UnregisterFromEvents()
{
process.OutputDataReceived -= process_OutputDataReceived;
process.ErrorDataReceived -= process_ErrorDataReceived;
} private void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
} private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
}
}

使用FFMPEG进行一些视频处理(C#)视频合并、转码、获取时长的更多相关文章

  1. 用阿里官网提供的plupload oss的web直传,视频上传进行前端验证它的时长,尺寸,大小等。替换上一个不需要的单个视频

    accessid = '' accesskey = '' host = '' policyBase64 = '' signature = '' callbackbody = '' filename = ...

  2. ffmreg thinkphp 控制器 获取音频视频详细信息(获取时长)

    FFmpeg下载:http://ffmpeg.zeranoe.com/builds/ 下载并解压FFmpeg文件夹: 打开你想安装的任意磁盘,例如:d盘.新建一个名为“ffmpeg”的文件夹,将第二步 ...

  3. 获取音、视频时长(NAudio,Shell32,FFmpeg)

    参考网址:https://blog.csdn.net/u013810234/article/details/57471780 以下为本次测试用到的音.视频格式: audio :”.wav;.mp3;. ...

  4. FFMpeg 常用命令格式转换,视频合成

    FFmpeg都是命令行的,用起来肯定不方便.但是,这对技术宅应该不成问题.下面,我就罗列一些比较实用的使用方法吧. FFmpeg的下载与安装 FFmpeg是开源的.但我们不必去下载它的源代码.下载已经 ...

  5. js 获取上传视频的时长、大小、后缀名

    参考资料:获取时长 var fileName = $("#sectionfileUpload").val(); //C:\fakepath\3.jpeg var exts = fi ...

  6. 以springMVC为例获取上传视频文件时长

    毕设项目是一个在线学习系统,教师用户有上传视频的功能,在答辩之前赶了一个demo出来,好多功能都写死了,比如课程学习进度就是被我写死在前端的一个变量,最近导师要我把项目打包发给他,这才心慌慌赶紧把这些 ...

  7. android获取mp4视频文件总时长和视频宽高<转>

    android使用 MediaMetadataRetriever 获取视频文件的 总时长 和视频的分辨率. 根据该方式获取视频信息可以看出不仅仅可以获取时长和分辨率,还能获取到其他的一些视频信息,不错 ...

  8. FFmpeg的使用——PHP转换视频、截取视频以及JW Player播放器控制

    转载:http://blog.csdn.net/zm2714/article/details/7916440 给朋友做的一个项目中,涉及到上传视频.转换视频.自动截取已上传视频内容中的一帧做为缩略图片 ...

  9. 新手学习FFmpeg - 调用API完成两个视频的任意合并

    本次尝试在视频A中的任意位置插入视频B. 在上一篇中,我们通过调整PTS可以实现视频的加减速.这只是对同一个视频的调转,本次我们尝试对多个视频进行合并处理. Concat如何运行 ffmpeg提供了一 ...

随机推荐

  1. CentOS.7下安装配置FTP和SFTP服务

    一: FTP Centos7中默认已经安装了sshd服务(sftp), vsftpd需要手动安装 1.安装并启动FTP服务 1.1 安装vsftpd 使用 yum 安装 vsftpd yum inst ...

  2. 通过Visual Studio 2012 比较SQL Server 数据库的架构变更

    一 需求 随着公司业务的发展,数据库实例也逐渐增多,数据库也会越来越多,有时候我们会发现正式生产数据库也测试数据库数据不一致,也有可能是预发布环境下的数据库与其他数据库架构不一致,或者,分布式数据库上 ...

  3. 9102 IT人保持记忆力及健康的方法

    做技术时间久了,我们会发现有的时候我们会感觉记忆力衰减太快,前脚刚忙完的事或者刚做完计划任务没多久就遗忘了,或者是以前轻车熟入的方法死活都记不起来了,亦或者之前学习一门技术很快就掌握真谛,现在即便花N ...

  4. Ubuntukylin 14.04 系统语言改成中文[转]

    1.在左侧点击"system setting" 2.按在图中方法设置 3.重启系统   参考地址:http://hi.baidu.com/thj2080/item/ae8e5dce ...

  5. Linux 下必备的性能检测工具 合集

    有些工具,值得学习学习: 网络 iftop IO  iotop 系统  top htop 保持更新,转载请注明出处. https://www.cnblogs.com/xuyaowen/p/linux- ...

  6. applicationSettings设置和appsttings

    applicationSettings 可以和sttings一样在配置文件中,设置参数.支持定义参数的类型“serializaAs=string”,并可以使用  . 语法. 可以使用.语法

  7. kunbernetes-快速入门

    1.入门概述 本文以在容器云上部署一个nexus3应用为例,通过通过一步步的操作,帮助用户快速的对Kubernetes有一个快速和整体的认识.通过快速入门,可以提供如下知识内容: 在集群中部署一个容器 ...

  8. [LeetCode] 10. 正则表达式匹配

    题目链接:https://leetcode-cn.com/problems/regular-expression-matching/ 题目描述: 给定一个字符串 (s) 和一个字符模式 (p).实现支 ...

  9. Linux实战教学笔记51:Zabbix监控平台3.2.4(三)生产环境案例

    https://www.cnblogs.com/chensiqiqi/p/9162986.html 一,Zabbix生产环境监测案例概述 1.1 项目规划 [x] :主机分组 交换机 Nginx To ...

  10. Vue slot插槽内容分发

    slot插槽使用 使用场景,一般父组件中又一大段模板内容需要运用到子组件上.或者更加复杂的,子组件需要运用到父组件大段模板内容,而子组件却不知道挂载的内容是什么.挂载点的内容是由父组件来决定的. Sl ...