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对视频截图的更多相关文章

  1. 使用ffmpeg 对视频截图,和视频转换格式

    //执行CMD命令方法 public static void CmdProcess(string command)//调用CMD        {            //实例化一个进程类      ...

  2. NET 2.0(C#)调用ffmpeg处理视频的方法

    另外:ffmpeg的net封装库 http://www.intuitive.sk/fflib/ NET 2.0 调用FFMPEG,并异步读取输出信息的代码...public void ConvertV ...

  3. Java调用FFmpeg进行视频处理及Builder设计模式的应用

    1.FFmpeg是什么 FFmpeg(https://www.ffmpeg.org)是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它用来干吗呢?视频采集.视频格式转化.视频 ...

  4. 使用ffmpeg进行视频截图

    1.从ffmpeg的Windows Build网站(https://ffmpeg.zeranoe.com/builds/)下载ffmpeg. 2.下载后解压压缩包,得到如下左图的文件.然后打开bin文 ...

  5. asp.net实现调用ffmpeg实现视频格式的转换

    视频格式转换的函数 //视频转换 public void VideoConvertFlv(string FromName, string ExportName) { string ffmpeg = H ...

  6. 用java程序调用ffmpeg执行视频文件格式转换flv

    用java小例题说明更直观:(可以直接编译运行)环境我在windows平台下测试的...需要在e:/下有ffmpeg.exe;mencoder.exe;drv43260.dll;pncrt.dll共4 ...

  7. java调用ffmpeg获取视频文件信息的一些参数

    一.下载ffmpeg http://www.ffmpeg.org/download.html 主要需要bin目录下的ffmpeg可执行文件 二.java代码实现 package com.aw.util ...

  8. bash shell,调用ffmpeg定期截图

    #!/bin/bash #获取当前目录中所有m3u8文件,并 var=$(ls |grep '.m3u8'|cut -d '.' -f1) #死循环 = ] do #循环每个文件 for stream ...

  9. windows下使用ffmpeg进行视频转换和截图。

    author:fanfq(xiaoban) Email:fangqing.fan#gmail.comlink:http://fanfq.iteye.com/admin/blogs/655569chan ...

随机推荐

  1. Java中基本数据类型、不能用浮点数表示金额

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10831763.html 一:8种基本数据类型 8种基本数据类型(4整,2浮,1符,1布): 整型:byte( ...

  2. window开机启动

    C:\Users\sunyues\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 再次文件夹下写脚本就可 @echo off ...

  3. opencv 程序

    IplImage结构中的一个元素:struct _IplROI *roi; //图像感兴趣区域,当该值非空时,只对该区域进行处理 .   ROI :Region of Interest,表示感兴趣的区 ...

  4. 201871010106-丁宣元 《2019面向对象程序设计(java)课程学习进度条》

    <2019面向对象程序设计(java)课程学习进度条> 周次 (阅读/编写)代码行数 发布博客量/评论他人博客数量 课余学习时间(小时) 学习收获最大的程序阅读或编程任务 1 25/10 ...

  5. 达信:深度解读COSO新版企业风险管理框架(ERM)

    http://www.sohu.com/a/124375769_489979 2016年6月,美国反欺诈财务报告委员会(The Committee of Sponsoring Organization ...

  6. 前端js判空处理,js字符串判空,js数组判空

    1.字符串 在 js 中,字符串为空会有这么几种形式,"",null,undefined,如果在已知变量为空串的情况下可以直接采用 if (string.length == 0) ...

  7. Linux下进程间通信方式——pipe(管道)

    每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程A把数据从用户空间拷到内核缓冲区,进程B再从内核缓冲区把 ...

  8. VQA视觉问答基础知识

    本文记录简单了解VQA的过程,目的是以此学习图像和文本的特征预处理.嵌入以及如何设计分类loss等等. 参考资料: https://zhuanlan.zhihu.com/p/40704719 http ...

  9. React - 组件:函数组件

    目录: . 组件名字首字母一定是大写的 . 返回一个jsx . jsx依赖React,所以组件内部需要引入React . 组件传参 a. 传递. <Component list={ arrDat ...

  10. ECMAScript6-2

    1.模板字串.箭头函数 1.1.模板字串 传统js,输出模板 var str='<b>姓名:</b>'+ '<span>lxr</span>'; con ...