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. SQLServer之修改标量值函数

    修改标量值函数注意事项 更改先前通过执行 CREATE FUNCTION 语句创建的现有 Transact-SQL 或 CLR 函数,但不更改权限,也不影响任何相关的函数.存储过程或触发器. 不能用 ...

  2. per-CPU变量

    为什么需要per-CPU变量 假设系统中有4个cpu, 同时有一个变量在各个CPU之间是共享的,每个cpu都有访问该变量的权限. 当cpu1在改变变量v的值的时候,cpu2也需要改变变量v的值.这时候 ...

  3. 高端内存映射之kmap持久内核映射--Linux内存管理(二十)

    1 高端内存与内核映射 尽管vmalloc函数族可用于从高端内存域向内核映射页帧(这些在内核空间中通常是无法直接看到的), 但这并不是这些函数的实际用途. 重要的是强调以下事实 : 内核提供了其他函数 ...

  4. luajit官方性能优化指南和注解

    luajit是目前最快的脚本语言之一,不过深入使用就很快会发现,要把这个语言用到像宣称那样高性能,并不是那么容易.实际使用的时候往往会发现,刚开始写的一些小test case性能非常好,经常毫秒级就算 ...

  5. 关于SNMP的MIB文件的语法简述

    源地址:https://blog.csdn.net/carechere/article/details/51236184 SNMP协议的MIB文件的常见宏定义的描述: 对MIB文件中一些常见的宏定义的 ...

  6. 解决ajax get post方式提交中文参数乱码问题

    最近在工作中遇到,使用ajax get方式提交中文参数的时候出现乱码,通过上网搜索,总结出比较简单的两种解决方案: 第一种,由于tomcat默认的字符集是ISO-8859-1,修改Tomcat中的se ...

  7. Javascrip 入门第三节课

    一.location对象 location.href 获取当前网页的URLlocation.search() 获取?之后的请求信息 location.href="URL" // 跳 ...

  8. jpa 分页

    public Page<Stability> testPager(){ Pageable pageable = new PageRequest(1, 10, Sort.Direction. ...

  9. [Oracle维护工程师手记]一次升级后运行变慢的分析

    客户报告,当他从 Oracle 11.1.0.7 ,迁移到云环境,并且升级到12.1.0.2.运行客户的应用程序测试,发现比以前更慢了. 从AWR report 的"Top 10 Foreg ...

  10. js把变量转换成json数据

    var a="";var MessageList=JSON.stringify(a);