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. 交叉编译用于生成aarch64指令的GCC (9.2)

    参考 Building GCC as a cross compiler for Raspberry Pi How to Build a GCC Cross-Compiler 环境 PC: ubuntu ...

  2. 使用docker创建mongodb

    1.创建 MongoDB 数据卷 docker volume create mongo_data_yapi 2.启动 MongoDB docker run -d --name mongo-yapi - ...

  3. Nginx 核心配置-location的匹配案例实战篇

    Nginx 核心配置-location的匹配案例实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.location语法规则介绍 在没有使用正则表达式的时候,nginx会先在 ...

  4. javascript中事件总结&通用的事件侦听器函数封装&事件委托

    前言: JAVASCRIPT与HTML之间的交互是通过事件来实现的.事件,就是文档或浏览器窗口中发生的一些特定交互瞬间.可以使用侦听器( 或处理程序 )来预定事件,以便事件发生时执行相应的代码.这种在 ...

  5. springmvc 整合 netty-socketio

    1 maven <dependency> <groupId>com.corundumstudio.socketio</groupId> <artifactId ...

  6. linux系统启动报错:[contains a file system with errors, check forced]的解决方法参考

    1.解决参考一Press enter for maintenance(or type Control-D to continue):/dev/sda3 contains a file system w ...

  7. arguments简单函数 求整数递加和

    function add(n){if(n == 1) return 1;else return n + arguments.callee(n-1);alert(arguments.callee(1)) ...

  8. 【Linux】Windows终端远程链接Linux服务器

    一.Windows cmd ssh链接 1.控制面板->程序->启用Telnet客户端 2.输入命令链接 cmd中输入 ssh 账号名@服务器ip地址:端口号 例如: ssh root@1 ...

  9. 学习-guava

    Guava Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库 例如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives sup ...

  10. es4x 引用外部jar 包以及集成typescrip

    以前写过一个通过修改jar 包处理自定义jar 的引入的,如下是一种使用官方推荐的方法package.json 添加依赖配置 同时为了方便使用添加typescript define 文件方便使用(只是 ...