【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)
ffmpeg-截图功能实现
1. ffmpeg 命令行方式
ffmpeg.exe 10 -i possible.mkv test.jpg
或
ffmpeg.exe -ss 10 -i possible.mkv -y -f image2 -t 0.01 0.jpg
或
ffmpeg.exe -ss 00:00:10 -i possible.mkv -y -f image2 -frames:v 1 0.jpg
命令参数说明:
-i: 选择输入文件, 如"-i possible.mkv"是指定ffmpeg.exe输入的媒体文件为possible.mkv
-ss: 选择开始时间, 如"-ss 10"是将视频指向10秒, 也就是从10秒开始
-y: 强制覆盖文件(防止因为重名出错)
-f: 指定输出的文件格式, 如"-f image2"
-t: 指定操作的持续时间("-t 0.01"相当于取原视频中的第10s~10.01秒), 一般用于截取视频使用, 而不是用在截图上.
"-frames:v 1" for a single image
用于替换-t选项, 上面的-t选项在截图中使用是不合理的. 该操作可以指定1张图片
0.jpg // 为输出文件
你可能会遇到的error:
[image2 @ 000002159e25b4c0] Could not get frame filename number 2 from pattern '0.jpg'.
Use '-frames:v 1' for a single image,
or '-update' option,
or use a pattern such as %03d within the filename.
av_interleaved_write_frame(): Invalid argument
原因:
你截图没有选定操作的个数, 加上-frames:v 1选项或者-t 0.01即可.
2. ffmpeg 代码方式
配套博文:
ffmpeg-struct SwsContext使用心得
PPM / PGM / PBM 图像格式
源码:
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <stdio.h>
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/pixfmt.h"
#include "libavutil/log.h"
}
/**
* 错误提示字符串
*/
#define ERR_STR_LEN 1024
static char err_buf[ERR_STR_LEN];
/**
* 用户自定义区
*/
#define START_FRAME 200 // 避免视频前面是黑的, 这有点坑人~
const char *file_path = "possible.mkv"; // 视频文件的路径, 支持linux/windows格式.
#define MYDEBUG qDebug() <<"[FILE:" <<__FILE__ <<",FUNC:" <<__FUNCTION__ <<",LINE:" <<__LINE__ <<"] "
void SaveFrame(AVFrame *pFrame, int width, int height,int index)
{
FILE *pFile;
char szFilename[32];
int x, y;
uint8_t *pTemp;
sprintf(szFilename, "debug/frame%d.ppm", index);
pFile=fopen(szFilename, "wb");
if(pFile == NULL)
return;
//写文件头, ppm格式: https://blog.youkuaiyun.com/MACMACip/article/details/105378600
fprintf(pFile, "P6 %d %d 255", width, height);
/**
* 写像素数据
* 使用for(y=0; y<height; y++){
* fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
* } 也可, 如果你的RGB排布刚好是正确的像素的话~
*/

本文详细介绍了如何使用FFmpeg进行视频截图,包括命令行方式和代码实现方式。通过实例展示了如何设置参数,如开始时间、输出文件格式和覆盖文件等,并解决了常见的截图错误。此外,还提供了基于Qt5和FFmpeg4.2.2的代码示例,演示了如何在C++中实现截图功能。
最低0.47元/天 解锁文章
3745

被折叠的 条评论
为什么被折叠?



