要不然需要修改location的地址为存放ffmpeg的地址
using FFMpegCore;
using FFMpegCore.Enums;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Reflection;
namespace FfmpegStudy
{
internal class Program
{
static void Main(string[] args)
{
var loaction = Assembly.GetExecutingAssembly().Location;
var folder = Directory.GetParent(loaction);
//设置ffmpeg和ffprobe的路径
GlobalFFOptions.Configure(new FFOptions { BinaryFolder = folder.FullName, TemporaryFilesFolder = folder.FullName });
var videoPath = @"E:\360MoveData\Users\Administrator\Desktop\视频测试\test\aaa.mp4";
// var videoOutPath = @"E:\360MoveData\Users\Administrator\Desktop\视频测试\test\aaa_1080.mp4";
var videoFile = new FileInfo(videoPath);
if (!videoFile.Exists)
{
Console.WriteLine("视频文件不存在");
Console.ReadKey();
return;
}
var mediaInfo = FFProbe.Analyse(videoPath);
if (mediaInfo.VideoStreams.Count < 1)
{
Console.WriteLine("获取视频信息失败");
Console.ReadKey();
}
Console.WriteLine(JsonConvert.SerializeObject(mediaInfo));
Console.ReadKey();
}
}
}
结果
{
"Duration":"00:01:37.0660000",
"Format":{
"Duration":"00:01:37.0660000",
"StartTime":"00:00:00",
"FormatName":"mov,mp4,m4a,3gp,3g2,mj2",
"FormatLongName":"QuickTime / MOV",
"StreamCount":2,
"ProbeScore":100,
"BitRate":37228816,
"Tags":{
"major_brand":"isom",
"minor_version":"512",
"compatible_brands":"isomiso2avc1mp41",
"creation_time":"2023-02-15T03:41:56.000000Z",
"Hw":"1",
"bitrate":"45000000",
"maxrate":"0",
"te_is_reencode":"1",
"encoder":"Lavf58.76.100"
}
},
"PrimaryAudioStream":{
"Channels":2,
"ChannelLayout":"stereo",
"SampleRateHz":44100,
"Profile":"LC",
"Index":1,
"CodecName":"aac",
"CodecLongName":"AAC (Advanced Audio Coding)",
"CodecTagString":"mp4a",
"CodecTag":"0x6134706d",
"BitRate":191803,
"StartTime":"00:00:00",
"Duration":"00:01:37.0360000",
"Language":"und",
"Disposition":{
"default":true,
"dub":false,
"original":false,
"comment":false,
"lyrics":false,
"karaoke":false,
"forced":false,
"hearing_impaired":false,
"visual_impaired":false,
"clean_effects":false,
"attached_pic":false,
"timed_thumbnails":false,
"captions":false,
"descriptions":false,
"metadata":false,
"dependent":false,
"still_image":false
},
"Tags":{
"creation_time":"2023-02-15T03:41:56.000000Z",
"language":"und",
"handler_name":"SoundHandler",
"vendor_id":"[0][0][0][0]"
},
"BitDepth":null
},
"PrimaryVideoStream":{
"AvgFrameRate":30.000000206043957,
"BitsPerRawSample":8,
"DisplayAspectRatio":{
"Item1":9,
"Item2":16
},
"SampleAspectRatio":{
"Item1":1,
"Item2":1
},
"Profile":"High",
"Width":2160,
"Height":3840,
"FrameRate":30,
"PixelFormat":"yuv420p",
"Rotation":0,
"AverageFrameRate":0,
"Index":0,
"CodecName":"h264",
"CodecLongName":"H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"CodecTagString":"avc1",
"CodecTag":"0x31637661",
"BitRate":37032413,
"StartTime":"00:00:00",
"Duration":"00:01:37.0660000",
"Language":"und",
"Disposition":{
"default":true,
"dub":false,
"original":false,
"comment":false,
"lyrics":false,
"karaoke":false,
"forced":false,
"hearing_impaired":false,
"visual_impaired":false,
"clean_effects":false,
"attached_pic":false,
"timed_thumbnails":false,
"captions":false,
"descriptions":false,
"metadata":false,
"dependent":false,
"still_image":false
},
"Tags":{
"creation_time":"2023-02-15T03:41:56.000000Z",
"language":"und",
"handler_name":"VideoHandler",
"vendor_id":"[0][0][0][0]"
},
"BitDepth":8
},
"PrimarySubtitleStream":null,
"VideoStreams":[
{
"AvgFrameRate":30.000000206043957,
"BitsPerRawSample":8,
"DisplayAspectRatio":{
"Item1":9,
"Item2":16
},
"SampleAspectRatio":{
"Item1":1,
"Item2":1
},
"Profile":"High",
"Width":2160,
"Height":3840,
"FrameRate":30,
"PixelFormat":"yuv420p",
"Rotation":0,
"AverageFrameRate":0,
"Index":0,
"CodecName":"h264",
"CodecLongName":"H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"CodecTagString":"avc1",
"CodecTag":"0x31637661",
"BitRate":37032413,
"StartTime":"00:00:00",
"Duration":"00:01:37.0660000",
"Language":"und",
"Disposition":{
"default":true,
"dub":false,
"original":false,
"comment":false,
"lyrics":false,
"karaoke":false,
"forced":false,
"hearing_impaired":false,
"visual_impaired":false,
"clean_effects":false,
"attached_pic":false,
"timed_thumbnails":false,
"captions":false,
"descriptions":false,
"metadata":false,
"dependent":false,
"still_image":false
},
"Tags":{
"creation_time":"2023-02-15T03:41:56.000000Z",
"language":"und",
"handler_name":"VideoHandler",
"vendor_id":"[0][0][0][0]"
},
"BitDepth":8
}
],
"AudioStreams":[
{
"Channels":2,
"ChannelLayout":"stereo",
"SampleRateHz":44100,
"Profile":"LC",
"Index":1,
"CodecName":"aac",
"CodecLongName":"AAC (Advanced Audio Coding)",
"CodecTagString":"mp4a",
"CodecTag":"0x6134706d",
"BitRate":191803,
"StartTime":"00:00:00",
"Duration":"00:01:37.0360000",
"Language":"und",
"Disposition":{
"default":true,
"dub":false,
"original":false,
"comment":false,
"lyrics":false,
"karaoke":false,
"forced":false,
"hearing_impaired":false,
"visual_impaired":false,
"clean_effects":false,
"attached_pic":false,
"timed_thumbnails":false,
"captions":false,
"descriptions":false,
"metadata":false,
"dependent":false,
"still_image":false
},
"Tags":{
"creation_time":"2023-02-15T03:41:56.000000Z",
"language":"und",
"handler_name":"SoundHandler",
"vendor_id":"[0][0][0][0]"
},
"BitDepth":null
}
],
"SubtitleStreams":[
],
"ErrorData":[
]
}
宽度高度和时长
var videoInfomation = mediaInfo.VideoStreams[0];
var videoWidth = videoInfomation.Width;
var videoHeight = videoInfomation.Height;
var dur = (int)videoInfomation.Duration.TotalSeconds
Console.WriteLine($"原始视频宽度:{videoWidth}\t高度:{videoHeight}");
using FFMpegCore;
using FFMpegCore.Enums;
using System;
using System.IO;
using System.Reflection;
namespace FfmpegStudy
{
internal class Program
{
static void Main(string[] args)
{
var loaction = Assembly.GetExecutingAssembly().Location;
var folder = Directory.GetParent(loaction);
GlobalFFOptions.Configure(new FFOptions { BinaryFolder = folder.FullName, TemporaryFilesFolder = folder.FullName });
var videoPath = @"E:\360MoveData\Users\Administrator\Desktop\视频测试\test\aaa.mp4";
// var videoOutPath = @"E:\360MoveData\Users\Administrator\Desktop\视频测试\test\aaa_1080.mp4";
var videoFile = new FileInfo(videoPath);
if (!videoFile.Exists)
{
Console.WriteLine("视频文件不存在");
Console.ReadKey();
return;
}
var mediaInfo = FFProbe.Analyse(videoPath);
if (mediaInfo.VideoStreams.Count < 1)
{
Console.WriteLine("获取视频信息失败");
Console.ReadKey();
}
var videoInfomation = mediaInfo.VideoStreams[0];
var videoWidth = videoInfomation.Width;
var videoHeight = videoInfomation.Height;
Console.WriteLine($"原始视频宽度:{videoWidth}\t高度:{videoHeight}");
//判断宽度为1080还是高度为1080
if (videoWidth > 1080 || videoHeight > 1080)
{
if (videoWidth >= videoHeight)
{
videoWidth = 1080;
videoHeight = (int)((1080.0 / videoWidth) * videoHeight);
if (videoHeight % 2 != 0)
{
videoHeight += 1;
}
}
else
{
videoWidth = (int)((1080.0 / videoHeight) * videoWidth);
if (videoWidth % 2 != 0)
{
videoWidth += 1;
}
videoHeight = 1080;
}
}
Console.WriteLine($"压缩后视频宽度:{videoWidth}\t高度:{videoHeight}");
var ffmpegArgs = FFMpegArguments.FromFileInput(videoPath)
.OutputToFile(videoPath, true,
options => options.WithVideoCodec(VideoCodec.LibX264)
.WithConstantRateFactor(28)
.WithAudioCodec(AudioCodec.Aac)
//.WithVariableBitrate(4)
.WithFramerate(20)
.WithVideoFilters(filterOptions => filterOptions
.Scale(videoWidth, videoHeight))//width和height不允许为单数
.WithFastStart());
//Console.WriteLine(ffmpegArgs.Arguments);//打印ffmpeg运行命令
ffmpegArgs.ProcessSynchronously();
Console.WriteLine("处理完毕...");
Console.ReadKey();
}
}
}