.net core+linux+ffmpeg安装与使用,获取视频截图
环境:
centos8
.net core 3.1
ffmpeg-5.0.1
原文链接:
ffmpeg安装:https://www.cnblogs.com/ysfurh/p/14274045.html
在.net core中使用ffmpeg:http://www.wjhsh.net/zhanghm1-p-13032207.html
一、 linux下ffmpeg安装与使用
1.官网下载ffmpeg
2.使用Xftp将下载的ffmpeg-5.0.1.tar.xz上传至linux主机的 /usr/local
3.解压文件
cd /usr/local #切换目录
tar -xvJf ffmpeg-5.0.1.tar.xz #解压
mv ffmpeg-5.0.1 ffmpeg #重命名
4.在ffmpeg目录下安装gcc编译器
cd /usr/local/ffmpeg
yum install gcc #安装gcc编译器
5.安装yasm编译器
cd /usr/local
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz #下载源码包
tar zxvf yasm-1.3.0.tar.gz #解压
cd yasm-1.3.0 #进入目录
./configure #配置
make && make install #编译安装
若提示-bash: wget: 未找到命令,请执行下面命令安装wget:
yum -y install wget
6.安装ffmpeg
cd /usr/local/ffmpeg
./configure --enable-shared --prefix=/usr/local/ffmpeg
make && make install #编译安装 ,过程比较慢
7. 修改文件/etc/ld.so.conf
sudo vim /etc/ld.so.conf
#输入i进入插入模式,将第二行的内容插入到该文件
include ld.so.conf.d/*.conf
/usr/local/ffmpeg/lib/
#按Esc键,输入 :wq! 强制退出保存
ldconfig
若提示 sudo: vim:找不到命令,,请执行下面命令安装:
yum -y install vim*
8. 配置环境变量
vim /etc/profile
#在最后PATH添加环境变量: #set ffmpeg environment PATH=$PATH:/usr/local/ffmpeg/bin export PATH
source /etc/profile #使配置生效
9.查看环境变量是否配置成功
ffmpeg -version
10.视频截图、读取视频信息
使用Xftp将一个mp4格式视频文件上传至linux主机 /home/test 下
视频截图:
ffmpeg -i /home/test/test.mp4 -y -q:v 7 -f image2 -t 0.001 /home/test/test.jpg
读取视频信息
ffprobe -i /home/test/test.mp4 -print_format json -show_format -show_streams -show_data
二、.net core下使用ffmpeg进行视频截图、读取视频信息
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace Myffmpeg.Common { public static class FfmpegHelper { private static System.Diagnostics.ProcessStartInfo cmdFfmpeg; private static System.Diagnostics.ProcessStartInfo cmdFfprobe; static FfmpegHelper() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { string ffmpegPath = "/usr/local/ffmpeg/ffmpeg"; string ffprobePath = "/usr/local/ffmpeg/ffprobe"; cmdFfmpeg = new System.Diagnostics.ProcessStartInfo(ffmpegPath); cmdFfprobe = new System.Diagnostics.ProcessStartInfo(ffprobePath); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { string ffmpegPath = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg\\ffmpeg.exe"; string ffprobePath = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg\\ffprobe.exe"; cmdFfmpeg = new System.Diagnostics.ProcessStartInfo(ffmpegPath); cmdFfprobe = new System.Diagnostics.ProcessStartInfo(ffprobePath); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { string ffmpegPath = "/usr/local/ffmpeg/ffmpeg"; string ffprobePath = "/usr/local/ffmpeg/ffprobe"; cmdFfmpeg = new System.Diagnostics.ProcessStartInfo(ffmpegPath); cmdFfprobe = new System.Diagnostics.ProcessStartInfo(ffprobePath); } cmdFfmpeg.RedirectStandardError = false; // 输出错误 cmdFfmpeg.RedirectStandardOutput = true; //输出打印 cmdFfmpeg.UseShellExecute = false; //使用Shell cmdFfmpeg.CreateNoWindow = true; //创建黑窗 cmdFfprobe.RedirectStandardError = false; //set false cmdFfprobe.RedirectStandardOutput = true; cmdFfprobe.UseShellExecute = false; //set true cmdFfprobe.CreateNoWindow = true; //don't need the black window } /// <summary> /// 获取视频信息 /// </summary> /// <param name="path"></param> public static async Task<string> GetVideoInfo(string path) { string command = $"-i {path} -print_format json -show_format -show_streams -show_data"; cmdFfprobe.Arguments = command; System.Diagnostics.Process cmd = new System.Diagnostics.Process(); cmd.StartInfo = cmdFfprobe; cmd.Start(); string InfoStr = await cmd.StandardOutput.ReadToEndAsync(); cmd.WaitForExit(); return InfoStr; } /// <summary> /// 视频截图 /// </summary> /// <param name="path"></param> /// <param name="outPath"></param> public static void VideoScreenshot(string path, string outPath) { string command = $"-i {path} -y -q:v 7 -f image2 -t 0.001 {outPath}"; cmdFfmpeg.Arguments = command; System.Diagnostics.Process cmd = new System.Diagnostics.Process(); cmd.StartInfo = cmdFfmpeg; cmd.Start(); cmd.WaitForExit(); } } }