Unity使用FFMpeg

news/2024/11/20 21:34:15/

Unity启动FFMpeg

private static bool StartUpFFmpeg(){bool isWinPlatform = true;if (Application.platform != RuntimePlatform.WindowsEditor &&Application.platform != RuntimePlatform.WindowsPlayer){isWinPlatform = false;}if (!isWinPlatform){string iOSPath = string.Empty;string extcutablesPath = string.Empty;if (Application.platform == RuntimePlatform.OSXEditor){iOSPath = Path.Combine(Application.dataPath, "CKTools/Plugins/FFMpeg/Mac/ffmpeg");extcutablesPath = Path.Combine(Application.dataPath, "CKTools/Plugins/FFMpeg/Mac");}else if (Application.platform == RuntimePlatform.OSXPlayer){iOSPath = Util.BuildsPath.Replace("YRBuilds", "FFMpeg/ffmpeg");extcutablesPath = Util.BuildsPath.Replace("YRBuilds", "FFMpeg");}sys_chmod(iOSPath, 755);FFmpeg.SetExecutablesPath(extcutablesPath, "ffmpeg", "ffmpeg");}else{string extcutablesPath = string.Empty;if (Application.platform == RuntimePlatform.WindowsEditor)extcutablesPath = Path.Combine(Application.dataPath, "CKTools/Plugins/FFMpeg/Windows");else if (Application.platform == RuntimePlatform.WindowsPlayer)extcutablesPath = Util.BuildsPath.Replace("YRBuilds", "FFMpeg");FFmpeg.SetExecutablesPath(extcutablesPath, "ffmpeg", "ffmpeg");}return true;}
使用ffmpeg将音频文件转为wav 16k 单声道
   public static async UniTask ConvertToWav(string inputFile, string outputPath = null){if (string.IsNullOrEmpty(outputPath)){outputPath = Path.Combine(Path.GetDirectoryName(inputFile),Path.GetFileNameWithoutExtension(inputFile) + "_bak.wav");}if (File.Exists(outputPath)) File.Delete(outputPath);if (!StartUpFFmpeg())return;string arguments = $"-i {inputFile} -vn -ar 16000 -ac 1 -b:a 16k -f wav -y {outputPath}";await FFmpeg.Conversions.New().Start(arguments);}

 对于图片的水平或者竖向分割

  public static async void SplitHalfOfPicture(string inputPath, string outPath1 = null, string outPath2 = null,string spliteType = "Ver"){if (!StartUpFFmpeg() || string.IsNullOrEmpty(inputPath))return;if (!File.Exists(inputPath)){YRLog.LogInfo("CKUtils SplitHalfOfPicture", $"文件不存在。路径:{inputPath}");return;}string extension = Path.GetExtension(inputPath);if (string.IsNullOrEmpty(outPath1))outPath1 = inputPath.Replace($".{extension}", $"split_1.{extension}");if (string.IsNullOrEmpty(outPath2))outPath2 = inputPath.Replace($".{extension}", $"split_2.{extension}");string arguments;if (spliteType.Equals("Ver"))arguments =$" -i \"{inputPath}\" -vf 'crop=iw/2:ih:0:0' -lossless 0 -quality 75 -y \"{outPath1}\" -vf 'crop=iw/2:ih:iw/2:0' -lossless 0 -quality 75 -y \"{outPath2}\"";elsearguments =$" -i \"{inputPath}\" -vf 'crop=iw:ih/2:0:0' -lossless 0 -quality 75 -y \"{outPath1}\" -vf 'crop=iw:ih/2:0:ih/2' -lossless 0 -quality 75 -y \"{outPath2}\"";await FFmpeg.Conversions.New().Start(arguments);}
图片大于1920*1080 = 2073600 进行压缩
   public static async UniTask ScalePicture(string inputPath, Action action = null){if (!StartUpFFmpeg())return;string suffix = Path.GetExtension(inputPath);string outPath = inputPath;if (!suffix.Equals(".jpg")){outPath = inputPath.Replace(suffix, ".jpg");if (File.Exists(outPath))File.Delete(outPath);File.Move(inputPath, outPath);}string tmpOutputPath = Path.Combine(Path.GetDirectoryName(outPath),Path.GetFileNameWithoutExtension(outPath) + "_bak.jpg");//-pix_fmt yuva422p  某些图片会如果色彩空间是 YUVA 通过命令会 YUV 色彩饱和度会增加 string arguments1 =$"-i \"{outPath}\" -vf \"scale='if(gt(ih*iw,2073600),iw/sqrt(ih*iw/2073600),iw)':'if(gt(ih*iw,2073600),ih/sqrt(ih*iw/2073600),ih)'\"  -y \"{tmpOutputPath}\" ";Debug.Log("ScalePicture ffmpeg:" + arguments1);await FFmpeg.Conversions.New().Start(arguments1);if (File.Exists(outPath)){try{File.Create(outPath).Dispose();File.Delete(outPath);}catch (Exception e){Console.WriteLine(e);throw;}}if (File.Exists(tmpOutputPath)){if (File.Exists(inputPath)){try{File.Create(inputPath).Dispose();File.Delete(inputPath);}catch (Exception e){Console.WriteLine(e);throw;}}File.Move(tmpOutputPath, inputPath);}action?.Invoke();}

压缩视频

  public static async UniTask CompressVideo(string inputPath){if (!StartUpFFmpeg())return;if (!File.Exists(inputPath))return;string rawPath = inputPath.Replace(".mp4", "_raw.mp4");if (File.Exists(rawPath))File.Delete(rawPath);File.Move(inputPath, rawPath);// string arguments = $"-i \"{rawPath}\" -s 1920*1080 -b:v 2000k -y \"{inputPath}\"";string arguments =$"-i \"{rawPath}\" -vf scale=-1:1080 -b:v {CKStatics.VIDEO_LIMIT_BITRATE}k -r 25 -c:v libx264 -profile:v main -level:v 4.0 -c:a copy -y \"{inputPath}\"";Debug.Log("CompressVideo ffmpeg:" + arguments);await FFmpeg.Conversions.New().Start(arguments);}
根据时间点 切割音频
 public static async UniTask SplitAudioByTime(string audioPath, List<float> times){if (!StartUpFFmpeg())return;if (times == null || times.Count <= 0)return;//需要对时间排序 如果顺序不对 FFmpeg会报错times.Sort((a, b) => (a.CompareTo(b)));string tempTimes = string.Empty;for (int i = 0; i < times.Count; i++){string time = SecondToHour(times[i]);if (i == times.Count - 1)tempTimes = $"{tempTimes}{time}";elsetempTimes = $"{tempTimes}{time},";}YRLog.LogInfo(TAG, "根据时间点 切割音频");string extension = Path.GetExtension(audioPath);string noExtensionPath = Path.GetFullPath(audioPath).Replace(extension, "");string arguments =$"-i \"{audioPath}\" -f segment -segment_times {tempTimes} -c copy -map 0 \"{noExtensionPath}_%d{extension}\"";YRLog.LogInfo(TAG, $"SplitAudioByTime arguments:{arguments}");await FFmpeg.Conversions.New().Start(arguments);}

压缩音频成为MP3

  public static async UniTask<string> CompressAudioToMp3(string inputPath){if (!StartUpFFmpeg())return inputPath;string dir = Path.GetDirectoryName(inputPath).Replace("\\", "/");string name = Path.GetFileNameWithoutExtension(inputPath);string outPath = $"{dir}/_fix{name}.mp3";string tempInputPath = string.Empty;string tempOutPath = string.Empty;if (CKStatics.toolModeType != ToolModeType.Course){tempInputPath = CKBookUtils.GetBookAbsolutePath(inputPath);tempOutPath = CKBookUtils.GetBookAbsolutePath(outPath);if (!File.Exists(tempInputPath)){CKToast.Instance.ToastShow("文件不存在");YRLog.LogInfo(TAG, $"{tempInputPath} 文件不存在");return inputPath;}}else{tempInputPath = inputPath;tempOutPath = outPath;}string arguments = $"-i {tempInputPath} -vn -ar 44100 -ac 2 -b:a 192k -acodec mp3 -y {tempOutPath}";YRLog.LogInfo(TAG, $"CompressAudioToMp3 参数 {arguments}");await FFmpeg.Conversions.New().Start(arguments);return outPath;}


http://www.ppmy.cn/news/615634.html

相关文章

硬件设计-PLL篇(下)

目录 概要 整体架构流程 技术名词解释 技术细节 1.环路滤波器采用有源滤波器还是无源滤波器&#xff1f;、 2.如何设计 VCO 输出功率分配器&#xff1f;、 3.如何设置电荷泵的极性&#xff1f; 4.锁定指示电路如何设计&#xff1f; 小结 概要 提示&#xff1a;这里可以添加技术…

分布式锁的实现方案(免费gpt4分享)

1.1基于数据库 有两个方案&#xff1a;依赖数据库排他锁以及表主键的唯一。 依赖数据库排他锁&#xff1a; 在查询语句后面增加for update&#xff0c;数据库会在查询过程中给数据库表增加排他锁 (注意&#xff1a; InnoDB 引擎在加锁的时候&#xff0c;只有通过索引进行检索…

python实现秒表计时器

秒表计时器 需求 利用python实现一个秒表计时器 2.能实现开始&#xff0c;停止&#xff0c;重置&#xff0c;退出功能 代码块 #from tkinter import * import time from re import X from tracemalloc import Frameclass StringVar(object):passdef Button(self, text, com…

单片机秒表c语言,单片机制作秒表计时器(c语言)

利用计数器中断,外部中断(按钮)编写的机遇c语言的秒表计时器 利用单片机制作秒表计时器 (c语言) #include//包含单片机对应的头文件 int MM0,SS0,MS0; int time2; unsigned int sc[10]{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned int dianyuan[6]{0xfe,0xf…

51单片机DIY_秒表计时器

计时表量程&#xff1a;00&#xff1a;00&#xff1a;00--59:59:99&#xff08;min&#xff1a;s&#xff1a;10ms&#xff09; 可存储和回查5组数据。 代码&#xff1a; #include<reg52.h> #include<string.h> /***********************定义管脚**************…

STC8H8k64U——定时器T0(60s倒计时)

60s倒计时 #include <STC8H.H> #include "delay.h"/*74HC245*/ #define OUTPUT P0/*3——8译码器*/ sbit A0 P2^2; sbit A1 P2^3; sbit A2 P2^4;unsigned char second 60; //秒计数 unsigned char count 200; //中断200次为1秒 unsigned char code LED_7…

单片机AT89C51六位(四位和两位)数码管秒表精度0.01s带启动、暂停、清零按钮

1.设计方案 本文主要研究基于单片机的秒表设计&#xff0c;主要是控制电路设计&#xff0c;数码管显示的设计&#xff0c;和软件程序的编写。该计时采用单片机定时器精确延时&#xff0c;秒表计时精度0.01秒。有启动&#xff0c;暂停&#xff0c;复位&#xff0c;提醒等功能。…

Python基础——time模块(制作秒表、倒计时)

time模块 利用Python完成自动化的任务&#xff0c;往往需要基于特定的时间和日期运行或定时运行。Python的内置的time模块便可以实现读取系统时钟的当前时间。time模块中最常用的就是time.time()和time.sleep()。 一、获取当前时间 time.time()函数 返回1970年1月1日0点以来…