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. rhel7 学习第二天

    参加<Linux就该这么学>在线培训的第二天,学习了虚拟环环境的搭建和红帽7的安装,同时也学习了rhel7的基本命令格式,以及systemctl的使用.

  2. soeasy的键盘鼠标事件

    在web自动化中,我们可能会遇到需要通过键盘或者鼠标去操作某些元素,那么我们就需要用到键盘事件和鼠标事件了,今天对键盘和鼠标操作进行一个总结 鼠标事件 鼠标事件需要引入ActionChains类,查看 ...

  3. centos7下搭建JDK和Hadoop

    涉及基础操作命令 这里只是将涉及到的提了下一下具体的使用还需要读者自己查阅资料 tar 解压命令 su 进入root用户模式 rm -rf 删除 cd /文件名/.../ 进入某个文件夹下 注意要逐层 ...

  4. socket小程序写一个客户端,实现给服务端发送hello World字符串,将客户端发送的数据变成大写后返回

    写一个客户端,实现给服务端发送hello World字符串,将客户端发送的数据变成大写后返回 本机id是192.168.xx.xy 服务端 import socket soc = socket.soc ...

  5. Graylog-Sidecar

    收集linux日志-filebeat 安装sidecar 下载graylog-sidecar-1.0.2-1.x86_64.rpm rpm -ivh graylog-sidecar-1.0.2-1.x ...

  6. 前端html页面,手机查看

    在写前端页面中,经常会在浏览器运行HTML页面,从本地文件夹中直接打开的一般都是file协议,当代码中存在http或https的链接时,HTML页面就无法正常打开,为了解决这种情况,需要在在本地开启一 ...

  7. CF308C-Sereja and Brackets-(线段树+括号匹配)

    题意:给出一段括号,多次询问某个区间内能匹配多少括号. 题解:线段树,结构体三个属性,多余的左括号l,多余的右括号r,能够匹配的括号数val. 当前结点的val=左儿子的val+右儿子的val+min ...

  8. 第05组 Beta冲刺(4/4)

    第05组 Beta冲刺(4/4) 队名:天码行空 组长博客连接 作业博客连接 团队燃尽图(共享): GitHub当日代码/文档签入记录展示(共享): 组员情况: 组员1:卢欢(组长) 过去两天完成了哪 ...

  9. CanvasRenderingContext2D.fillText(text, x, y [, maxWidth]);

    CanvasRenderingContext2D.fillText(text, x, y [, maxWidth]); [, maxWidth]的意思是,方括号代表可有可无,有fillText(tex ...

  10. 网络协议 8 - TCP协议(上)

    上次说了“性本善”的 UDP 协议,这哥们秉承“网之初,性本善,不丢包,不乱序”的原则,徜徉在网络世界中.     与之相对应的,TCP 就像是老大哥一样,了解了社会的残酷,变得复杂而成熟,秉承“性恶 ...