gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
如何通过 FFmpeg.AutoGen 传递参数给FFmpeg.exe 获取时长
- User
如何通过 FFmpeg.AutoGen 传递参数给FFmpeg.exe 获取时长
- Assistant
以下是使用FFmpeg.AutoGen和FFmpeg.exe获取视频时长的示例代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FFmpeg.AutoGen; namespace FFmpegTest { class Program { static void Main(string[] args) { // 初始化FFmpeg ffmpeg.av_register_all(); AVFormatContext* pFormatContext = null; // 打开视频文件 if (ffmpeg.avformat_open_input(&pFormatContext, "test.mp4", null, null) != 0) { Console.WriteLine("Error opening input file."); return; } // 获取视频信息 if (ffmpeg.avformat_find_stream_info(pFormatContext, null) < 0) { Console.WriteLine("Error finding stream info."); return; } // 遍历所有流 for (int i = 0; i < pFormatContext->nb_streams; i++) { AVStream* pStream = pFormatContext->streams[i]; // 如果流类型是视频 if (pStream->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) { // 获取时长 double duration = pStream->duration / (double)AV_TIME_BASE; Console.WriteLine("Video duration: " + duration + "s"); } } // 关闭文件 ffmpeg.avformat_close_input(&pFormatContext); Console.ReadLine(); } } } - User
我使用的ffmpeg.auto 5.0版本,提示 ffmpeg未包含av_register_all
- Assistant
在FFmpeg.AutoGen 5.0版本中,已经移除了
av_register_all()函数,因此不再需要调用它来初始化FFmpeg库。在新版本中,初始化FFmpeg库的方式变成了:
ffmpeg.avformat_network_init(); // 用于使用网络协议的功能 ffmpeg.avfilter_register_all(); // 用于使用音视频滤镜的功能所以您可以使用以下代码来初始化FFmpeg库,并获取视频时长:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FFmpeg.AutoGen; namespace FFmpegTest { class Program { static void Main(string[] args) { // 初始化FFmpeg ffmpeg.avformat_network_init(); ffmpeg.avfilter_register_all(); AVFormatContext* pFormatContext = null; // 打开视频文件 if (ffmpeg.avformat_open_input(&pFormatContext, "test.mp4", null, null) != 0) { Console.WriteLine("Error opening input file."); return; } // 获取视频信息 if (ffmpeg.avformat_find_stream_info(pFormatContext, null) < 0) { Console.WriteLine("Error finding stream info."); return; } // 遍历所有流 for (int i = 0; i < pFormatContext->nb_streams; i++) { AVStream* pStream = pFormatContext->streams[i]; // 如果流类型是视频 if (pStream->codecpar->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) { // 获取时长 double duration = pStream->duration / (double)AV_TIME_BASE; Console.WriteLine("Video duration: " + duration + "s"); } } // 关闭文件 ffmpeg.avformat_close_input(&pFormatContext); Console.ReadLine(); } } }