.Net调用ffmpeg对视频截图
2019/10/27, .Net c#代码片段
摘要:借助ffmpeg对视频/图片截图、生成缩略图,使用命令行调用ffmpeg工具,支持Linux和Windows
网上很多版本都是需要等待4s的做法,这里不需要等待固定4s
添加引用,此命名空间用于对系统类型进行判断,选取不同的命令对象:
using System.Runtime.InteropServices;
/// <summary>
/// 借助ffmpeg生成缩略图
/// </summary>
/// <param name="originalFilePath">源文件</param>
public void GenerateThumbnail(string originalFilePath)
{
try
{
//判断系统类型
//如果是windows,直接使用ffmpeg.exe
//如果是linux,则使用安装的ffmpeg(需要提前安装)
/*
Linux工具调用:ffmpeg -i 333.jpg -q:v 31 -frames:v 1 -y image.jpg
windows: ffmpeg.exe -i 333.jpg -q:v 31 -frames:v 1 -y image.jpg
-i 333.jpg 是输入文件
-q:v 31 是质量,值区间是2-31
-frames:v 1 是提取帧必要参数
-y 是遇到同名文件则覆盖
image.jpg 输出文件名
还可以加 -s 160*100 表示输出宽高比为160*100
*/
string outputFilePath = "image.jpg";//输出文件
string cmdPath = string.Empty;//ffmpeg工具对象
string cmdParams = $" -i {originalFilePath} -q:v 31 -frames:v 1 -y {outputFilePath} ";//命令参数
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
cmdPath = "ffmpeg.exe";//根据实际的ffmpeg.exe文件路径来
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
cmdPath = "ffmpeg";//安装ffmpeg工具
}
else
{
throw new Exception("当前操作系统不支持!");
}
using (System.Diagnostics.Process ffmpegProcess = new System.Diagnostics.Process())
{
StreamReader errorReader; // StringWriter to hold output from ffmpeg
// execute the process without opening a shell
ffmpegProcess.StartInfo.UseShellExecute = false;
//ffmpegProcess.StartInfo.ErrorDialog = false;
ffmpegProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// redirect StandardError so we can parse it
ffmpegProcess.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)
ffmpegProcess.StartInfo.FileName = cmdPath;
// 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)
ffmpegProcess.StartInfo.Arguments = cmdParams;
ffmpegProcess.Start();// start the process
// now that the process is started, we can redirect output to the StreamReader we defined
errorReader = ffmpegProcess.StandardError;
ffmpegProcess.WaitForExit();// wait until ffmpeg comes back
//result = errorreader.ReadToEnd();
}
}
catch (Exception ex)
{
throw new Exception("生成缩略图出错!", ex);
}
}
.Net调用ffmpeg对视频截图的更多相关文章
- 使用ffmpeg 对视频截图,和视频转换格式
//执行CMD命令方法 public static void CmdProcess(string command)//调用CMD { //实例化一个进程类 ...
- NET 2.0(C#)调用ffmpeg处理视频的方法
另外:ffmpeg的net封装库 http://www.intuitive.sk/fflib/ NET 2.0 调用FFMPEG,并异步读取输出信息的代码...public void ConvertV ...
- Java调用FFmpeg进行视频处理及Builder设计模式的应用
1.FFmpeg是什么 FFmpeg(https://www.ffmpeg.org)是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它用来干吗呢?视频采集.视频格式转化.视频 ...
- 使用ffmpeg进行视频截图
1.从ffmpeg的Windows Build网站(https://ffmpeg.zeranoe.com/builds/)下载ffmpeg. 2.下载后解压压缩包,得到如下左图的文件.然后打开bin文 ...
- asp.net实现调用ffmpeg实现视频格式的转换
视频格式转换的函数 //视频转换 public void VideoConvertFlv(string FromName, string ExportName) { string ffmpeg = H ...
- 用java程序调用ffmpeg执行视频文件格式转换flv
用java小例题说明更直观:(可以直接编译运行)环境我在windows平台下测试的...需要在e:/下有ffmpeg.exe;mencoder.exe;drv43260.dll;pncrt.dll共4 ...
- java调用ffmpeg获取视频文件信息的一些参数
一.下载ffmpeg http://www.ffmpeg.org/download.html 主要需要bin目录下的ffmpeg可执行文件 二.java代码实现 package com.aw.util ...
- bash shell,调用ffmpeg定期截图
#!/bin/bash #获取当前目录中所有m3u8文件,并 var=$(ls |grep '.m3u8'|cut -d '.' -f1) #死循环 = ] do #循环每个文件 for stream ...
- windows下使用ffmpeg进行视频转换和截图。
author:fanfq(xiaoban) Email:fangqing.fan#gmail.comlink:http://fanfq.iteye.com/admin/blogs/655569chan ...
随机推荐
- 让configure和cmake编译时支持调试选项
在Linux先编译软件基本都是采用configure文件生成makefile,或者,cmake生成makefile文件两种方式.它们生成的makefile文件,一般默认不支持-g调试选项.但我们使用这 ...
- 手写xpath定位公式
做web自动化,之前我们已经将环境搭建好了,现在的话总结下怎么定位元素的 最基本的元素定位是有6种: driver.find_element_by_id("") driver.fi ...
- javascript打开窗口
项目中javascript代码,早期使用了只有ie支持的方法:Window createPopup() 方法 那个时候是2009年,而现在已经是2019-12-11了.如何改造这个早期的代码呢? 找到 ...
- C#中的函数(一) 无参无返回值的函数
分析下C#中的函数 先写一个小例子,一个静态函数,无返回值,无形参 在第17行与20行分别下断点 F5调试运行,此时中断在第17行MyFunction(), 在第17行右键反汇编,看下反汇编代码 这里 ...
- 前端js判空处理,js字符串判空,js数组判空
1.字符串 在 js 中,字符串为空会有这么几种形式,"",null,undefined,如果在已知变量为空串的情况下可以直接采用 if (string.length == 0) ...
- Taro-UI 2.0样式在H5上生效,微信小程序不生效?
答案: https://taro-ui.aotu.io/#/docs/questions taro-ui 自定义样式覆盖小程序组件样式使用到了 globalClass 这个微信小程序特性,由于微信小程 ...
- xshell跳转设置 Xshell代理设置
本机------->A(中转)------>B(目标服务器) 本机---------XXXXX------>B(目标服务器) 本机无法直接连接B服务器 第一步:本机连接中转服务器A, ...
- Random Access Iterator 徐州网络赛(树形dp)
Random Access Iterator \[ Time Limit: 4000 ms \quad Memory Limit: 262144 kB \] 题意 给出伪代码,问按着伪代码在树上跑,能 ...
- qt ubutun各个版本下载地址
http://download.qt.io/archive/qt/ http://mirrors.melbourne.co.uk/ubuntu-releases/
- 直接插入排序与缩小增量插入排序(希尔排序ShellSort)
直接插入排序 要理解shell排序,首先要把直接插入排序的基础打扎实. 学习资料:白话经典算法系列之二 直接插入排序的三种实现.直接插入排序 根据我的思路,直接插入排序设置3重循环. 循环1:对 i= ...