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. 如何设置非管理员用户配置特定的IIS站点

    如何设置非管理员用     户配置特定的IIS站点 一.           添加IIS管理服务 二.           启动管理服务 勾选启用远程连接后.点右边的应用 三.           设 ...

  2. python 之 查找某目录中最新的文件

    记录一下这个方法,感觉很有用!>.< import os def find_newest_file(path_file): lists = os.listdir(path_file) li ...

  3. HTML,CSS---问题记录

    1,,登录框input和标签垂直方向对不齐,咋解决? 给input框外套一层span标签,给span标签设置宽高,让它和左边或右边的标签对齐. 不要直接给input设置宽高,这样是对不齐的 2,套没有 ...

  4. c# 判断一个string[]是否全包含另一个string[]

    // list = normalList.Except(repairList).ToList(); //差集 // list = normalList.Union(repairList).ToList ...

  5. SQL 撤销索引、表以及数据库

    通过使用 DROP 语句,可以轻松地删除索引.表和数据库. SQL DROP INDEX 语句 我们可以使用 DROP INDEX 命令删除表格中的索引. 用于 Microsoft SQLJet (以 ...

  6. Cookie Session 与Token

    由于HTTP是一种无状态的协议,服务器端无法知道用户与客户端交互的状态,比如如果一个用于之前已经访问过该服务器,服务器无法知道该用户是第二次访问,Session和Cookie都是用来保存用户与后端服务 ...

  7. SpringBoot前端模板

    Springboot支持thymeleaf.freemarker.JSP,但是官方不建议使用JSP,因为有些功能会受限制,这里介绍thymeleaf和freemarker. 一.thymeleaf模板 ...

  8. CF1012B Chemical table

    $CF1012B Chemical table 给你一个 \(n\times m\) 的矩形,一开始有 \(q\) 个格子上被标记.对于任意两行两列,如果交汇的四个格子中有三个被标记,那么第 \(4\ ...

  9. Eclipse中的快捷键

    Ctrl+1:快捷修复(数字 1 不是字母 l) 将鼠标悬停到出错区域,按 Ctrl+1,出现快捷修复的菜单, 按上下方向键选择一种修复方式即可. 也可以将光标移动到出错区域,按 F2 + Enter ...

  10. jconsole连接本地进程报安全连接失败

    连接本地程序报错 在idea工具中添加如下命令 -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.port=888 ...