c#.net 如何使用ffmpeg截图

在.net项目中经常遇到上传完视频需要自动对某一帧进行截图,这里贴出使用ffmpeg进行自动截图的方法

首先下载ffmpeg.exe文件

https://pan.baidu.com/s/1GS9ISoZvKfs9GA7_szvvlw

具体写法如下

String from = "C:\\WWW\\1530423892.mp4";
String to = "C:\\WWW\\1530423892.png";

string ffmpegPath = Server.MapPath("~/ffmpeg/ffmpeg.exe");//指定上传文件在服务器上的保存路径
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpegPath);
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.Arguments = "-ss 00:01:16 -i  " + from + " -f image2 -y " + to;
System.Diagnostics.Process.Start(startInfo);

其中笔者是把fmpeg直接放在网站根目录下的,所以文件路径采用相对路径

from和to两个路径 分别代表视频的绝对路径和截图后图片的保存路径 01:16表示截图的时间,可根据自己需要进行调整


 

你可以使用 .NET Core 库来使用 FFmpeg。其中一个流行的库是 FFmpeg.AutoGen,它提供了一个 .NET Core 版本的 FFmpeg API。通过使用这个库,你可以在 C#使用 FFmpeg 的功能,例如音频和视频编解码、剪辑和过滤等等。 下面是一个使用 FFmpeg.AutoGen 库来提取视频帧的简单示例: ``` using FFmpeg.AutoGen; // 初始化 FFmpeg ffmpeg.av_register_all(); // 打开视频文件 AVFormatContext* formatContext = null; ffmpeg.avformat_open_input(&formatContext, "video.mp4", null, null); ffmpeg.avformat_find_stream_info(formatContext, null); // 找到视频流 AVCodec* codec = null; int videoStreamIndex = ffmpeg.av_find_best_stream(formatContext, AVMediaType.AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0); // 打开视频解码器 AVCodecContext* codecContext = ffmpeg.avcodec_alloc_context3(codec); ffmpeg.avcodec_parameters_to_context(codecContext, formatContext->streams[videoStreamIndex]->codecpar); ffmpeg.avcodec_open2(codecContext, codec, null); // 循环提取视频帧 AVPacket packet = default(AVPacket); ffmpeg.av_init_packet(&packet); AVFrame* frame = ffmpeg.av_frame_alloc(); while (ffmpeg.av_read_frame(formatContext, &packet) >= 0) { if (packet.stream_index == videoStreamIndex) { ffmpeg.avcodec_send_packet(codecContext, &packet); while (ffmpeg.avcodec_receive_frame(codecContext, frame) == 0) { // 处理视频帧 } } ffmpeg.av_packet_unref(&packet); } // 释放资源 ffmpeg.av_frame_free(&frame); ffmpeg.avcodec_close(codecContext); ffmpeg.avformat_close_input(&formatContext); ``` 这个示例演示了如何使用 FFmpeg.AutoGen 库来打开一个视频文件,找到视频流,打开视频解码器并循环提取视频帧。你可以根据你的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值