.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 ...
随机推荐
- JMETER 使用断言
断言概念 断言就是在执行某个请求后,根据返回的结果,判断返回是否正确,如果不正确,则表示事务失败. 添加断言 启动流程时返回的数据是一个 json对象,结构为 {success:true,msg:&q ...
- 系统管理员必须知道的PHP安全实践
Apache web 服务器提供了这种便利 :通过 HTTP 或 HTTPS 协议,访问文件和内容.配置不当的服务器端脚本语言会带来各种各样的问题.所以,使用 PHP 时要小心.以下是 25 个 PH ...
- Listary 文件操作神器
文档简介 Listary 官方文档(中文版):https://zhuanlan.zhihu.com/p/24897629 视频介绍:https://v.youku.com/v_show/id_XNTY ...
- 前端(1)HTML介绍
1.1 HTML介绍 1.1Web服务本质 服务端 import socket server = socket.socket() server.bind(("127.0.0.1", ...
- 浏览器bug html 底部
- ajax中什么时候进success和error
先简单介绍下ajax: 简单的说Ajax请求通过XMLHttpRequest对象发送请求,该对象有四个状态(readyState): -未初始化.-正在初始化.-发送数据.-正在发送数据.-完成. 当 ...
- servlet中的doGet()与doPost()以及service()的用法
doget和dopost的区别 get和post是http协议的两种方法,另外还有head, delete等 1.这两种方法有本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能 ...
- ibatis实现分页查询
最近在做老项目改造,分享一个之前写的ibatis(这里特指ibatis2.x的版本)分页插件. 大致原理就是通过重写SqlExecutor的executeQuery方法,实现分页查询,支持mysql和 ...
- Java 解压 zip 文件
代码如下 package test_java; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcep ...
- Java查询MySQL数据库指定数据库中所有表名、字段名、字段类型、字段长度、字段描述
1,查询方法 public static List<Map<String, String>> getColumnInfoByTableName(String databaseN ...