之前写过ffmpeg录制桌面(自己用gdi抓图)
帧率是固定的。
现在决定写个变帧率的例子,第一分钟的帧率为10,第二分钟的帧率为5. 这个设置AVFrame的pts即可。 第一分钟,pts大致如下(以毫秒为单位): 100,200,300,400,500,600,700,800,900,1000,…
第二分钟,pts大致如下(以毫秒为单位): 60200,60400,60600,60800,61000
录制后,在播放时,会发现帧率为7,如下所示:
下面是代码结构 CaptureScreen.h内容如下:
#ifndef _CCAPTURE_SCREEN_HH
#define _CCAPTURE_SCREEN_HH
#include<time.h>
#include <d3d9.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <windows.h>
#include <tchar.h>
#include <winbase.h>
#include <winreg.h>
#include <Strsafe.h>
class CCaptureScreen
{
public:
CCaptureScreen(void);
~CCaptureScreen(void);
public:
int Init(int&, int&);
BYTE* CaptureImage();
private:
void* CaptureScreenFrame(int, int, int, int);
HCURSOR FetchCursorHandle();
private:
int m_width;
int m_height;
UINT wLineLen;
DWORD dwSize;
DWORD wColSize;
HDC hScreenDC;
HDC hMemDC;
PRGBTRIPLE m_hdib;
BITMAPINFO pbi;
HBITMAP hbm;
HCURSOR m_hSavedCursor;
};
#endif
CaptureScreen.cpp的内容如下:
#include "CaptureScreen.h"
CCaptureScreen::CCaptureScreen(void)
{
m_hdib = NULL;
m_hSavedCursor = NULL;
hScreenDC = NULL;
hMemDC = NULL;
hbm = NULL;
m_width = 1920;
m_height = 1080;
FetchCursorHandle();
}
CCaptureScreen::~CCaptureScreen(void)
{
DeleteObject(hbm);
if (m_hdib){
free(m_hdib);
m_hdib = NULL;
}
if (hScreenDC){
::ReleaseDC(NULL, hScreenDC);
}
if (hMemDC) {
DeleteDC(hMemDC);
}
if (hbm)
{
DeleteObject(hbm);
}
}
int CCaptureScreen::Init(int& src_VideoWidth, int& src_VideoHeight)
{
hScreenDC = ::GetDC(GetDesktopWindow());
if (hScreenDC == NULL) return 0;
int m_nMaxxScreen = GetDeviceCaps(hScreenDC, HORZRES);
int m_nMaxyScreen = GetDeviceCaps(hScreenDC, VERTRES);
hMemDC = ::CreateCompatibleDC(hScreenDC);
if (hMemDC == NULL) return 0;
m_width = m_nMaxxScreen;
m_height = m_nMaxyScreen;
if (!m_hdib){
m_hdib = (PRGBTRIPLE)malloc(m_width * m_height * 3);
}
pbi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbi.bmiHeader.biWidth = m_width;
pbi.bmiHeader.biHeight = m_height;
pbi.bmiHeader.biPlanes = 1;
pbi.bmiHeader.biBitCount = 24;
pbi.bmiHeader.biCompression = BI_RGB;
src_VideoWidth = m_width;
src_VideoHeight = m_height;
hbm = CreateCompatibleBitmap(hScreenDC, m_width, m_height);
SelectObject(hMemDC, hbm);
wLineLen = ((m_width * 24 + 31) & 0xffffffe0) / 8;
wColSize = sizeof(RGBQUAD)* ((24 <= 8) ? 1 << 24 : 0);
dwSize = (DWORD)(UINT)wLineLen * (DWORD)(UINT)m_height;
return 1;
}
BYTE* CCaptureScreen::CaptureImage()
{
VOID* alpbi = CaptureScreenFrame(0, 0, m_width, m_height);
return (BYTE*)(alpbi);
}
void* CCaptureScreen::CaptureScreenFrame(int left, int top, int width, int height)
{
if (hbm == NULL || hMemDC == NULL || hScreenDC == NULL) return NULL;
BitBlt(hMemDC, 0, 0, width, height, hScreenDC, left, top, SRCCOPY);
{
POINT xPoint;
GetCursorPos(&xPoint);
HCURSOR hcur = FetchCursorHandle();
xPoint.x -= left;
xPoint.y -= top;
ICONINFO iconinfo;
BOOL ret;
ret = GetIconInfo(hcur, &iconinfo);
if (ret){
xPoint.x -= iconinfo.xHotspot;
xPoint.y -= iconinfo.yHotspot;
if (iconinfo.hbmMask) DeleteObject(iconinfo.hbmMask);
if (iconinfo.hbmColor) DeleteObject(iconinfo.hbmColor);
}
::DrawIcon(hMemDC, xPoint.x, xPoint.y, hcur);
}
PRGBTRIPLE hdib = m_hdib;
if (!hdib)
return hdib;
GetDIBits(hMemDC, hbm, 0, m_height, hdib, (LPBITMAPINFO)&pbi, DIB_RGB_COLORS);
return hdib;
}
HCURSOR CCaptureScreen::FetchCursorHandle()
{
if (m_hSavedCursor == NULL)
{
m_hSavedCursor = GetCursor();
}
return m_hSavedCursor;
}
VaryFrameTest.cpp的内容如下:
#include "CaptureScreen.h"
extern "C"
{
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil\time.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#include <libavutil/imgutils.h>
#include <libavcodec/avcodec.h>
#include <libavdevice\avdevice.h>
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")
}
#include <chrono>
unsigned char clip_value(unsigned char x, unsigned char min_val, unsigned char max_val) {
if (x > max_val) {
return max_val;
}
else if (x < min_val) {
return min_val;
}
else {
return x;
}
}
bool RGB24_TO_YUV420(unsigned char *RgbBuf, int w, int h, unsigned char *yuvBuf)
{
unsigned char*ptrY, *ptrU, *ptrV, *ptrRGB;
memset(yuvBuf, 0, w*h * 3 / 2);
ptrY = yuvBuf;
ptrU = yuvBuf + w * h;
ptrV = ptrU + (w*h * 1 / 4);
unsigned char y, u, v, r, g, b;
for (int j = h - 1; j >= 0; j--) {
ptrRGB = RgbBuf + w * j * 3;
for (int i = 0; i < w; i++) {
b = *(ptrRGB++);
g = *(ptrRGB++);
r = *(ptrRGB++);
y = (unsigned char)((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
u = (unsigned char)((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v = (unsigned char)((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
*(ptrY++) = clip_value(y, 0, 255);
if (j % 2 == 0 && i % 2 == 0) {
*(ptrU++) = clip_value(u, 0, 255);
}
else {
if (i % 2 == 0) {
*(ptrV++) = clip_value(v, 0, 255);
}
}
}
}
return true;
}
DWORD WINAPI ScreenCapThreadProc(LPVOID lpParam)
{
CCaptureScreen* ccs = new CCaptureScreen();
int width = 0;
int height = 0;
ccs->Init(width, height);
AVFormatContext* avFormCtx_Out;
AVCodecContext* avCodecCtx_Out;
AVCodec* avCodec;
AVStream* avStream;
AVFrame* frame;
AVPacket* packet;
int frameRate = 10;
int ret = 0;
const char* filename = "out.mp4";
ret = avformat_alloc_output_context2(&avFormCtx_Out, NULL, NULL, filename);
if (ret < 0)
{
printf("Init avformat object is faild! \n");
return 0;
}
avCodec = (AVCodec *)avcodec_find_encoder(avFormCtx_Out->oformat->video_codec);
if (!avCodec)
{
printf("Init avCodec object is faild! \n");
return 0;
}
avCodecCtx_Out = avcodec_alloc_context3(avCodec);
if (!avCodecCtx_Out)
{
printf("Init avCodecCtx_Out object is faild! \n");
return 0;
}
avStream = avformat_new_stream(avFormCtx_Out, avCodec);
if (!avStream)
{
printf("Init avStream object is faild! \n");
return 0;
}
avCodecCtx_Out->flags |= AV_CODEC_FLAG_QSCALE;
avCodecCtx_Out->bit_rate = 4000000;
avCodecCtx_Out->rc_min_rate = 4000000;
avCodecCtx_Out->rc_max_rate = 4000000;
avCodecCtx_Out->bit_rate_tolerance = 4000000;
avCodecCtx_Out->time_base.den = frameRate;
avCodecCtx_Out->time_base.num = 1;
avCodecCtx_Out->framerate.den = frameRate;
avCodecCtx_Out->framerate.num = 1;
avCodecCtx_Out->width = width;
avCodecCtx_Out->height = height;
avCodecCtx_Out->gop_size = 12;
avCodecCtx_Out->max_b_frames = 0;
avCodecCtx_Out->thread_count = 4;
avCodecCtx_Out->pix_fmt = AV_PIX_FMT_YUV420P;
avCodecCtx_Out->codec_id = AV_CODEC_ID_H264;
avCodecCtx_Out->codec_type = AVMEDIA_TYPE_VIDEO;
av_opt_set(avCodecCtx_Out->priv_data, "b-pyramid", "none", 0);
av_opt_set(avCodecCtx_Out->priv_data, "preset", "superfast", 0);
av_opt_set(avCodecCtx_Out->priv_data, "tune", "zerolatency", 0);
if (avFormCtx_Out->oformat->flags & AVFMT_GLOBALHEADER)
avCodecCtx_Out->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
ret = avcodec_open2(avCodecCtx_Out, avCodec, NULL);
if (ret < 0)
{
printf("Open avcodec is faild! \n");
return 0;
}
avcodec_parameters_from_context(avStream->codecpar, avCodecCtx_Out);
if (!(avFormCtx_Out->oformat->flags & AVFMT_NOFILE))
{
ret = avio_open(&avFormCtx_Out->pb, filename, AVIO_FLAG_WRITE);
if (ret < 0)
{
printf("Open file is faild! \n");
return 0;
}
}
ret = avformat_write_header(avFormCtx_Out, NULL);
if (ret < 0)
{
printf("write header is faild! \n");
return 0;
}
frame = av_frame_alloc();
if (!frame)
{
printf("Init frame is faild! \n");
return 0;
}
frame->format = AV_PIX_FMT_YUV420P;
frame->width = width;
frame->height = height;
LONG64 frameSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, 1920, 1080, 1);
BYTE* outbuffer = new BYTE[frameSize];
ret = av_image_fill_arrays(frame->data,
frame->linesize,
outbuffer,
AV_PIX_FMT_YUV420P,
1920,
1080, 1);
if (ret < 0)
{
printf("av_image_fill_arrays is faild! \n");
return 0;
}
packet = av_packet_alloc();
if (!packet)
{
printf("packet is faild! \n");
return 0;
}
int frameNumber = 0;
int got_packet = 0;
DWORD dwBeginTime = ::GetTickCount();
for (;;)
{
BYTE* frameimage = ccs->CaptureImage();
RGB24_TO_YUV420(frameimage, width, height, outbuffer);
AVRational timeBaseMillsecond;
timeBaseMillsecond.den = 1000;
timeBaseMillsecond.num = 1;
DWORD dwCurrDtsTime = ::GetTickCount();
frame->pkt_dts = frame->pts = av_rescale_q_rnd(dwCurrDtsTime - dwBeginTime, timeBaseMillsecond, avStream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
frame->pkt_duration = 0;
frame->pkt_pos = -1;
ret = avcodec_send_frame(avCodecCtx_Out, frame);
if (ret < 0)
continue;
ret = avcodec_receive_packet(avCodecCtx_Out, packet);
if (ret < 0)
continue;
static DWORD dwInitTime = ::GetTickCount();
if (packet->size > 0)
{
av_write_frame(avFormCtx_Out, packet);
frameNumber++;
printf("录入第%d帧....\n", frameNumber);
}
DWORD dwCurrentTime = ::GetTickCount();
if (dwCurrentTime - dwInitTime > 120000)
{
break;
}
int dwPassedMillSeconds = dwCurrentTime - dwBeginTime;
if (dwCurrentTime - dwInitTime <= 60000)
{
int dwDiff = frameNumber * 1000 / 10 - dwPassedMillSeconds;
if (dwDiff > 0)
{
Sleep(dwDiff);
}
}
else
{
int dwPassedMillSeconds = dwCurrentTime - dwBeginTime - 60000;
int dwDiff = (frameNumber - 600) * 1000 / 5 - dwPassedMillSeconds;
if (dwDiff > 0)
{
Sleep(dwDiff);
}
}
}
av_write_trailer(avFormCtx_Out);
avformat_free_context(avFormCtx_Out);
avcodec_close(avCodecCtx_Out);
avcodec_free_context(&avCodecCtx_Out);
av_free(avCodec);
av_packet_free(&packet);
av_frame_free(&frame);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
avdevice_register_all();
HANDLE hThread = CreateThread(NULL, 0, ScreenCapThreadProc, 0, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
return 0;
}
|