当前位置: 首页 > news >正文

在.net中运用ffmpeg 操作视频

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.IO;

using System.Text;

namespace learun.util

{

/// <summary>

/// ffmpeg视频相关处理的类

/// </summary>

public class FFmpegUtil

{

public static int Run(string cmd)

{

try

{

//string ffmpeg = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"FFmpeg\ffmpeg.exe";

string ffmpeg = ConfigHelper.GetConfig().MultimediaFile.Replace("/", "\\") + @"FFmpeg\ffmpeg.exe";

ProcessStartInfo startInfo = new ProcessStartInfo(ffmpeg);

startInfo.UseShellExecute = false;

startInfo.CreateNoWindow = true;

startInfo.WindowStyle = ProcessWindowStyle.Hidden;

startInfo.Arguments = cmd;

Process process = Process.Start(startInfo);

//process.WaitForExit(3000);

process.Kill();

return 1;

}

catch (Exception ex)

{

return -1;

}

}

public static int Run1(string cmd)

{

int r = 0;

try

{

//string ffmpeg = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"FFmpeg\ffmpeg.exe";

string ffmpeg = ConfigHelper.GetConfig().MultimediaFile.Replace("/", "\\") + @"FFmpeg\ffmpeg.exe";

Process p = new Process();

p.StartInfo.FileName = ffmpeg;

p.StartInfo.Arguments = cmd;

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardError = true;

p.StartInfo.CreateNoWindow = true;

string a = string.Empty;

p.ErrorDataReceived += new DataReceivedEventHandler((s, message) =>

{

//Response.Write(message.Data);

a = message.Data;

});//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN

p.Start();//启动线程

p.BeginErrorReadLine();//开始异步读取

p.WaitForExit();//阻塞等待进程结束

p.Close();//关闭进程

p.Dispose();//释放资源

r = 1;

}

catch (Exception ex)

{

r = -1;

}

return r;

}

/// <summary>

/// 按时间获取某帧图片

/// </summary>

/// <param name="videoPath">视频路径</param>

/// <param name="outPath">输出路径</param>

/// <param name="frameTime">时间(格式:00:00:01)</param>

public static void GetFrame(string videoPath, string outPath, string frameTime)

{

Run(string.Format("-ss 00:00:01 -i {1} {2}", frameTime, videoPath, outPath));

}

/ <summary>

/ 批量添加图片水印

/ </summary>

/ <param name="videoPath"></param>

/ <param name="outPath"></param>

/ <param name="listImg"></param>

//public static void AddListImageMark(string videoPath, string outPath, List<ImgMark> listImg)

//{

// string imgs = "", postions = "";

// foreach (ImgMark mark in listImg)

// {

// imgs += " -i " + mark.ImgPath;

// postions += "overlay=" + mark.Postion.X + ":" + mark.Postion.Y + ",";

// }

// postions = postions.Remove(postions.Length - 1);

// Run(string.Format("-i {0}{1} -filter_complex \"{2}\" {3}", videoPath, imgs, postions, outPath));

//}

/ <summary>

/ 批量添加图片水印

/ </summary>

/ <param name="videoPath"></param>

/ <param name="outPath"></param>

/ <param name="listImg"></param>

//public static void AddImageMark(string videoPath, string outPath, ImgMark img)

//{

// Run(string.Format("-i {0} -i {1} -filter_complex overlay {2}", videoPath, img.ImgPath, outPath));

// //./ ffmpeg -i input.mp4 -i iQIYI_logo.png - filter_complex overlay output.mp4

//}

/ <summary>

/ 添加文字水印

/ </summary>

/ <param name="videoPath">视频路径</param>

/ <param name="outPath">输出路径</param>

/ <param name="textMark">水印属性</param>

//public static void AddTextMark(string videoPath, string outPath, TextMark textMark)

//{

// Run(string.Format(" -i {0} -vf \"drawtext=fontfile={1}: text='{2}':x={3}:y={4}:fontsize={5}:fontcolor={6}\" {7}", videoPath, textMark.FontFile, textMark.Text, textMark.X, textMark.Y, textMark.FontSize, textMark.FontColor.Name.ToLower(), outPath));

// //@"%{localtime\:%Y\-%m\-%d %H-%M-%S}"

//}

/// <summary>

/// 获取视频时长

/// </summary>

/// <param name="sourceFile"></param>

/// <returns></returns>

public static int GetVideoDuration(string sourceFile)

{

//string ffmpegfile = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"FFmpeg\ffmpeg.exe";

string ffmpegfile = ConfigHelper.GetConfig().MultimediaFile.Replace("/", "\\") + @"FFmpeg\ffmpeg.exe";

try

{

using (System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process())

{

String duration; // soon will hold our video's duration in the form "HH:MM:SS.UU"

String result; // temp variable holding a string representation of our video's duration

StreamReader errorreader; // StringWriter to hold output from ffmpeg

// we want to execute the process without opening a shell

ffmpeg.StartInfo.UseShellExecute = false;

//ffmpeg.StartInfo.ErrorDialog = false;

ffmpeg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

// redirect StandardError so we can parse it

// for some reason the output comes through over StandardError

ffmpeg.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)

ffmpeg.StartInfo.FileName = ffmpegfile;

// 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)

ffmpeg.StartInfo.Arguments = "-i " + sourceFile;

// start the process

ffmpeg.Start();

// now that the process is started, we can redirect output to the StreamReader we defined

errorreader = ffmpeg.StandardError;

// wait until ffmpeg comes back

ffmpeg.WaitForExit();

// read the output from ffmpeg, which for some reason is found in Process.StandardError

result = errorreader.ReadToEnd();

// a little convoluded, this string manipulation...

// working from the inside out, it:

// takes a substring of result, starting from the end of the "Duration: " label contained within,

// (execute "ffmpeg.exe -i somevideofile" on the command-line to verify for yourself that it is there)

// and going the full length of the timestamp

duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length);

string[] ss = duration.Split(':');

int h = int.Parse(ss[0]);

int m = int.Parse(ss[1]);

int s = int.Parse(ss[2]);

return h * 3600 + m * 60 + s;

}

}

catch (System.Exception ex)

{

//记录日志

return -1;

}

}

/// <summary>

/// 图片合成视频

/// </summary>

/// <param name="alljpgPath">所有的图片路径</param>

/// <param name="outputMp4Path">输出合成图片视频的所有路径</param>

public static void ImageConvertVideo(string alljpgPath, string outputMp4Path)

{

//string cmd = string.Format(" -i {0} -f segment -segment_time {1} -c copy {2}", targetMp3path, time, outputmp3Path);

//string jpgpath = @"D:\yunyunWork\xinjiang\TestMultiMedia\WindowsFormsApp1\bin\Debug\李梦真\%2d.jpg";

//string mp4path = pathString + string.Format(@"FFmpeg\vedio\o{0}.mp4", Guid.NewGuid().ToString());

string cmd = string.Format("-r 1 -f image2 -i {0} -vcodec libx264 -s 640*480 -g 1 -keyint_min 1 -sc_threshold 0 -pix_fmt yuv420p {1}", alljpgPath, outputMp4Path);

Run1(cmd);

}

/// <summary>

/// 分隔音乐时长

/// </summary>

/// <param name="targetMp3path">目标mp3文件</param>

/// <param name="time">分隔时长</param>

/// <param name="outputmp3Path">输出的目录如</param>

public static void SeparateMusicDuration(string targetMp3path, int time, string outputmp3Path)

{

string cmd = string.Format(" -i {0} -f segment -segment_time {1} -c copy {2}", targetMp3path, time, outputmp3Path);

Run1(cmd);

}

/// <summary>

/// 视频时长大于音乐时长,视频中轮询播放mp3

/// </summary>

/// <param name="inputMp4Path"></param>

/// <param name="mp3Path"></param>

/// <param name="outputMp4Path"></param>

public static void VideoLoopMusic(string inputMp4Path, string mp3Path, string outputMp4Path)

{

string cmd = string.Format("-i {0} -stream_loop -1 -i {1} -filter_complex [0:a][1:a]amix -t 60 -y {2}", inputMp4Path, mp3Path, outputMp4Path);

Run(cmd);

}

/// <summary>

/// 视频中添加音乐

/// </summary>

/// <param name="mp3path">mp3所在的路径</param>

/// <param name="targetMp4Path">需要添加音乐的视频</param>

/// <param name="outputMp4Path">输出添加音乐后的视频</param>

public static void VideoAddMusic(string mp3path, string targetMp4Path, string outputMp4Path)

{

string cmd = string.Format(" -i {0} -i {1} -y {2}", mp3path, targetMp4Path, outputMp4Path);

Run1(cmd);

}

}

}

相关文章:

在.net中运用ffmpeg 操作视频

using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Text;namespace learun.util{/// <summary>/// ffmpeg视频相关处理的类/// </summary>public class FFmpegUtil{public static int Run(string cmd){try{//…...

05- 线性回归算法 (LinearRegression) (算法)

线性回归算法(LinearRegression)就是假定一个数据集合预测值与实际值存在一定的误差, 然后假定所有的这些误差值符合正太分布, 通过方程求这个正太分布的最小均值和方差来还原原数据集合的斜率和截距。当误差值无限接近于0时, 预测值与实际值一致, 就变成了求误差的极小值。 fr…...

JAVA补充知识01之枚举enum

目录 1. 枚举类的使用 1.1 枚举类的理解 1.2 举例 1.3 开发中的建议&#xff1a; 1.4 Enum中的常用方法 1.5 熟悉Enum类中常用的方法 1.6 枚举类实现接口的操作 1.7 jdk5.0之前定义枚举类的方式 &#xff08;了解即可&#xff09; 1.8 jdk5.0之后定义枚举类的方式 1…...

jenkins下配置maven

1. 先在jenkins服务器上安装maven 下载-解压-重命名-启动 [rootVM-0-12-centos local]# wget https://mirrors.aliyun.com/apache/maven/maven-3/3.9.0/binaries/apache-maven-3.9.0-bin.tar.gz [rootVM-0-12-centos local]# tar xf apache-maven-3.9.0-bin.tar.gz [rootVM-0…...

春季开学即将到来!大学生活必备数码清单奉上

马上就要开学了&#xff0c;你的返校装备是否已经准备齐全了呢&#xff1f;对于高校学生来说&#xff0c;很多数码产品都属于必备装备&#xff0c;比如下面这几款产品就受到了大量年轻消费者的喜爱&#xff0c;在它们的帮助下能够让大家的学习时光变得更快乐。1、不入耳黑科技骨…...

ubuntu18.04 天选2 R95900hx 3060显卡驱动安装

天选2 R95900hx 3060显卡驱动安装需求问题解决内核集显显卡驱动需求 外接显示器&#xff0c;安装nvidia驱动 问题 由于一开始直接在软件和更新中附加读懂安装了nvidia-470&#xff0c;导致系统黑屏。 解决 grub页面系统选择进入ubuntu recovery模式&#xff0c;选择root&a…...

Harbor安装部署实战详细手册

文章目录前言一、安装docker二、安装docker-compose1.下载2.赋权3.测试三、安装harbor1.下载2.解压3.修改配置文件4.部署5.配置开机自启动6.登录验证7.补充说明四、harbor使用问题1.docker login问题&#xff1a;Error response from daemon: Get https://: http: server gave …...

华为OD机试真题JAVA实现【箱子之形摆放】真题+解题思路+代码(20222023)

🔥系列专栏 华为OD机试(JAVA)真题目录汇总华为OD机试(Python)真题目录汇总华为OD机试(C++)真题目录汇总华为OD机试(JavaScript)真题目录汇总文章目录 🔥系列专栏题目输入输出描述示例一输入输出说明备注解题思路Code运行结果版权说明...

华为OD机试 - 事件推送(Python)| 真题+思路+考点+代码+岗位

事件推送 题目 同一个数轴 X 上有两个点的集合 A={A1, A2, …, Am} 和 B={B1, B2, …, Bn}, Ai 和 Bj 均为正整数,A、B 已经按照从小到大排好序,A、B 均不为空, 给定一个距离 R (正整数), 列出同时满足如下条件的所有(Ai, Bj)数对: Ai <= BjAi, Bj 之间的距离小于…...

【Linux】信号量

&#x1f387;Linux&#xff1a; 博客主页&#xff1a;一起去看日落吗分享博主的在Linux中学习到的知识和遇到的问题博主的能力有限&#xff0c;出现错误希望大家不吝赐教分享给大家一句我很喜欢的话&#xff1a; 看似不起波澜的日复一日&#xff0c;一定会在某一天让你看见坚持…...

android-java同步方法和异步方法

接口 Java接口是一系列方法的声明&#xff0c;是一些方法特征的集合&#xff0c;一个接口只有方法的特征没有方法的实现&#xff0c;因此这些方法可以在不同的地方被不同的类实现&#xff0c;而这些实现可以具有不同的行为&#xff08;功能&#xff09;。 两种含义&#xff1a…...

Flask入门(5):请求和响应

目录5.请求和响应5.1 请求5.2 响应5.请求和响应 5.1 请求 request对象封装解析了请求报文中的数据&#xff0c;其大部分功能是由依赖包werkzeug完成的&#xff0c;并且每个request对象都是线程隔离的&#xff0c;保证了数据的安全性。 request对象的属性 1.request.method …...

记进组后第五次组会汇报

2023年2月14日 日记一、小组组会二、实验室组会1、汇报内容&#xff08;1&#xff09;参考文献&#xff08;2&#xff09;CQF机制a.研究现状b.相关思考&#xff08;3&#xff09;研究计划2、汇报反馈一、小组组会 上午十点整&#xff0c;小组组会开始&#xff0c;有两个同学我…...

nil Foundation的Placeholder证明系统(2)

前序博客&#xff1a; nil Foundation的Placeholder证明系统&#xff08;1&#xff09; nil; Foundation团队2022年11月论文《Placeholder证明系统》。[2022年11月29日版本] 8. 优化 8.1 Batched FRI 不同于单独检查每个commitment&#xff0c;可对其进行FRI聚合。如对多项…...

QHash源码解读

QT版本 v5.12.10 元素 // 重点说明QHashData的函数&#xff0c;QHashData是QHash的基础 struct QHashData {struct Node {Node *next;uint h;};Node *fakeNext; // 永为nullNode **buckets; // Node *数组QtPrivate::RefCount ref;int size; // node个数int nodeSize; /…...

【Unity细节】RigidBody中Dynamic和Kinematic的区别

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! 本文由 秩沅 原创 收录于专栏&#xff1a;unity细节和bug ⭐Dynamic和Kinematic的区别⭐ 文章目录⭐Dynamic和Kinematic的区别⭐&#x1f3…...

【C++、数据结构】哈希 — 闭散列与哈希桶的模拟实现

文章目录&#x1f4d6; 前言1. STL中哈希表的两个应用⚡1.1 &#x1f31f;unordered_set1.2 &#x1f31f;unordered_map2. 常见查找的性能对比&#x1f4a5;3. 哈希表模拟实现&#x1f3c1;3.1 哈希的概念&#xff1a;3.2 哈希函数&#xff1a;3.3 哈希冲突&#xff1a;3.4 闭…...

vue 开发环境 卸载node 版本 切换新的 node 版本 mac电脑

注意&#xff1a;操作的机器当前是mac&#xff0c;先卸载&#xff0c;再安装 1.查看现有 node 版本 node -v2.卸载现有 node 版本&#xff0c; 1.卸载从node官网下载pkg安装的node sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node…...

在Linux和Windows上安装Nacos-2.1.1

记录&#xff1a;377场景&#xff1a;在CentOS 7.9操作系统安装Nacos-2.1.1。在Windows操作系统上安装Nacos-2.1.1。Nacos&#xff1a;Nacos: Dynamic Naming and Configuration Service。Nacos提供动态配置服务、服务发现及管理、动态DNS服务功能。版本&#xff1a;JDK 1.8 Na…...

解决QML debugging is enabled.Only use this in a safe environment.警告

系列文章目录 文章目录系列文章目录前言一、警告原因二、解决办法参考前言 我试图运行一个非常简单的程序&#xff0c;当单击退出按钮时关闭窗口&#xff0c;但获取以下输出&#xff0c;前提是包含按钮的应用程序窗口不显示&#xff1a; 您已启用QML调试(实际上它默认启用)&…...

java_网络服务相关_gateway_nacos_feign区别联系

1. spring-cloud-starter-gateway 作用&#xff1a;作为微服务架构的网关&#xff0c;统一入口&#xff0c;处理所有外部请求。 核心能力&#xff1a; 路由转发&#xff08;基于路径、服务名等&#xff09;过滤器&#xff08;鉴权、限流、日志、Header 处理&#xff09;支持负…...

.Net框架,除了EF还有很多很多......

文章目录 1. 引言2. Dapper2.1 概述与设计原理2.2 核心功能与代码示例基本查询多映射查询存储过程调用 2.3 性能优化原理2.4 适用场景 3. NHibernate3.1 概述与架构设计3.2 映射配置示例Fluent映射XML映射 3.3 查询示例HQL查询Criteria APILINQ提供程序 3.4 高级特性3.5 适用场…...

[ICLR 2022]How Much Can CLIP Benefit Vision-and-Language Tasks?

论文网址&#xff1a;pdf 英文是纯手打的&#xff01;论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误&#xff0c;若有发现欢迎评论指正&#xff01;文章偏向于笔记&#xff0c;谨慎食用 目录 1. 心得 2. 论文逐段精读 2.1. Abstract 2…...

Spring Boot面试题精选汇总

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Spring Boot面试题精选汇总⚙️ **一、核心概…...

Unity | AmplifyShaderEditor插件基础(第七集:平面波动shader)

目录 一、&#x1f44b;&#x1f3fb;前言 二、&#x1f608;sinx波动的基本原理 三、&#x1f608;波动起来 1.sinx节点介绍 2.vertexPosition 3.集成Vector3 a.节点Append b.连起来 4.波动起来 a.波动的原理 b.时间节点 c.sinx的处理 四、&#x1f30a;波动优化…...

安卓基础(aar)

重新设置java21的环境&#xff0c;临时设置 $env:JAVA_HOME "D:\Android Studio\jbr" 查看当前环境变量 JAVA_HOME 的值 echo $env:JAVA_HOME 构建ARR文件 ./gradlew :private-lib:assembleRelease 目录是这样的&#xff1a; MyApp/ ├── app/ …...

Spring是如何解决Bean的循环依赖:三级缓存机制

1、什么是 Bean 的循环依赖 在 Spring框架中,Bean 的循环依赖是指多个 Bean 之间‌互相持有对方引用‌,形成闭环依赖关系的现象。 多个 Bean 的依赖关系构成环形链路,例如: 双向依赖:Bean A 依赖 Bean B,同时 Bean B 也依赖 Bean A(A↔B)。链条循环: Bean A → Bean…...

IP如何挑?2025年海外专线IP如何购买?

你花了时间和预算买了IP&#xff0c;结果IP质量不佳&#xff0c;项目效率低下不说&#xff0c;还可能带来莫名的网络问题&#xff0c;是不是太闹心了&#xff1f;尤其是在面对海外专线IP时&#xff0c;到底怎么才能买到适合自己的呢&#xff1f;所以&#xff0c;挑IP绝对是个技…...

现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?

现有的 Redis 分布式锁库&#xff08;如 Redisson&#xff09;相比于开发者自己基于 Redis 命令&#xff08;如 SETNX, EXPIRE, DEL&#xff09;手动实现分布式锁&#xff0c;提供了巨大的便利性和健壮性。主要体现在以下几个方面&#xff1a; 原子性保证 (Atomicity)&#xff…...

AI+无人机如何守护濒危物种?YOLOv8实现95%精准识别

【导读】 野生动物监测在理解和保护生态系统中发挥着至关重要的作用。然而&#xff0c;传统的野生动物观察方法往往耗时耗力、成本高昂且范围有限。无人机的出现为野生动物监测提供了有前景的替代方案&#xff0c;能够实现大范围覆盖并远程采集数据。尽管具备这些优势&#xf…...