项目开始设计的是运行在windows下,所以一开始采用的是windows服务模式来获取多媒体文件信息,后来要求调整为可以在Linux下运行,经过这两天的资料查找,实现了Linux下通过.NET来获取多媒体文件信息的功能。详情如下:
1、运行环境是CentOS 7
2、编译环境是VS2017+.NET Core2.2
项目引用 MediaInfo.DotNetWrapper
CentOS下安装以下环境(已安装过的可忽略):
sudo ln -s /usr/lib64/libmediainfo.so.0 /usr/lib64/libmediainfo.so
sudo yum install https://mediaarea.net/download/binary/mediainfo/18.08.1/mediainfo-18.08.1.x86_64.CentOS_7.rpm
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
sudo yum install dotnet-sdk-2.2
运行代码如下:
using log4net;
using log4net.Config;
using log4net.Repository;
using MediaInfo.DotNetWrapper;
using MediaInfo.DotNetWrapper.Enumerations;
using System;
using System.IO;
namespace Services
{
public class Program
{
protected static ILog log = LogManager.GetLogger(logRepository.Name, "Program");
public static void Main(string[] args)
{
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
using (var mediaInfo =new MediaInfo.DotNetWrapper.MediaInfo())
{
mediaInfo.Open("123.mpg");
string FileType = mediaInfo.Get(StreamKind.Video, 0, "StreamKind");
int s = 0;
int.TryParse(mediaInfo.Get(StreamKind.Video, 0, "Duration"), out s);
log.Debug($"文件类型:{FileType}");
log.Debug($"视频长度:{s}");
}
}
}
}
后台运行并且不随终端断开而结束的运行方式:dotnet xxxx.dll&