gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
如何通过 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(); } } }在上面的代码中,我们打开了一个名为“test.mp4”的视频文件。通过遍历所有流,我们可以找到视频流并获取其时长。最后我们关闭输入文件并打印出视频时长。
您可以通过修改“test.mp4”的路径和文件名来适应您的需求。
- 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(); } } }在上面的代码中,我们使用了AVCodecParameters结构体中的
codec_type属性来判断流类型,这是因为在FFmpeg 4.x及以后的版本中,AVCodecContext结构体已经被弃用,取而代之的是AVCodecParameters结构体。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.