ffmpeg-截图功能实现

本文详细介绍了如何使用FFmpeg进行视频截图,包括命令行方式和代码实现方式。通过实例展示了如何设置参数,如开始时间、输出文件格式和覆盖文件等,并解决了常见的截图错误。此外,还提供了基于Qt5和FFmpeg4.2.2的代码示例,演示了如何在C++中实现截图功能。
【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) 

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 代码方式


托管平台: github
项目名: Hiboat
commit: 4e9aefc25bf5118f075c9cb7bbe6f3c93ade1528
message: 实现ppm图片的截图功能
环境: qt5 + ffmpeg4.2.2

配套博文:
  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排布刚好是正确的像素的话~
     */
    
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安河桥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值