Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <windows.h>
- #include <stdint.h>
- extern "C" {
- #include <libavcodec/avcodec.h>
- #include <libavformat/avformat.h>
- #include <libswscale/swscale.h>
- #include <libavutil/opt.h>
- #include <libavutil/imgutils.h>
- #include <libavutil/error.h>
- }
- void printFFmpegError(int errNum) {
- char errbuf[AV_ERROR_MAX_STRING_SIZE];
- av_strerror(errNum, errbuf, AV_ERROR_MAX_STRING_SIZE);
- std::cerr << "FFmpeg error: " << errbuf << std::endl;
- }
- // Funktion für die Aufnahme eines Screenshots
- std::vector<uint8_t> captureScreenshot(int& width, int& height) {
- HDC hScreenDC = GetDC(NULL); // Desktop DC
- HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
- width = GetSystemMetrics(SM_CXSCREEN);
- height = GetSystemMetrics(SM_CYSCREEN);
- HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
- HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
- BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
- BITMAPINFOHEADER bi = {0};
- bi.biSize = sizeof(BITMAPINFOHEADER);
- bi.biWidth = width;
- bi.biHeight = -height; // Negative to avoid flipping the image
- bi.biPlanes = 1;
- bi.biBitCount = 24;
- bi.biCompression = BI_RGB;
- std::vector<uint8_t> buffer(width * height * 3); // RGB buffer
- GetDIBits(hMemoryDC, hBitmap, 0, height, buffer.data(), (BITMAPINFO*)&bi, DIB_RGB_COLORS);
- SelectObject(hMemoryDC, hOldBitmap);
- DeleteObject(hBitmap);
- DeleteDC(hMemoryDC);
- ReleaseDC(NULL, hScreenDC);
- return buffer;
- }
- std::vector<uint8_t> compressWithH264(const std::vector<uint8_t>& rgbData, int width, int height) {
- std::vector<uint8_t> compressedData;
- const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264); // Hier const verwenden
- if (!codec) {
- std::cerr << "Codec not found!" << std::endl;
- return compressedData;
- }
- AVCodecContext* codecCtx = avcodec_alloc_context3(codec);
- if (!codecCtx) {
- std::cerr << "Could not allocate video codec context!" << std::endl;
- return compressedData;
- }
- codecCtx->bit_rate = 400000;
- codecCtx->width = width;
- codecCtx->height = height;
- codecCtx->time_base = (AVRational){1, 25};
- codecCtx->framerate = (AVRational){25, 1};
- codecCtx->gop_size = 10;
- codecCtx->max_b_frames = 1;
- codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
- if (avcodec_open2(codecCtx, codec, NULL) < 0) {
- std::cerr << "Could not open codec!" << std::endl;
- avcodec_free_context(&codecCtx);
- return compressedData;
- }
- AVFrame* frame = av_frame_alloc();
- frame->format = AV_PIX_FMT_YUV420P;
- frame->width = width;
- frame->height = height;
- if (av_image_alloc(frame->data, frame->linesize, width, height, AV_PIX_FMT_YUV420P, 32) < 0) {
- std::cerr << "Could not allocate raw picture buffer!" << std::endl;
- av_frame_free(&frame);
- avcodec_free_context(&codecCtx);
- return compressedData;
- }
- SwsContext* swsCtx = sws_getContext(width, height, AV_PIX_FMT_RGB24, width, height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL);
- const uint8_t* inData[1] = { rgbData.data() };
- int inLinesize[1] = { 3 * width };
- sws_scale(swsCtx, inData, inLinesize, 0, height, frame->data, frame->linesize);
- frame->pts = 0;
- AVPacket* pkt = av_packet_alloc();
- if (!pkt) {
- std::cerr << "Could not allocate AVPacket!" << std::endl;
- sws_freeContext(swsCtx);
- av_frame_free(&frame);
- avcodec_free_context(&codecCtx);
- return compressedData;
- }
- if (avcodec_send_frame(codecCtx, frame) < 0) {
- std::cerr << "Error sending frame for encoding!" << std::endl;
- }
- if (avcodec_receive_packet(codecCtx, pkt) == 0) {
- compressedData.assign(pkt->data, pkt->data + pkt->size);
- } else {
- std::cerr << "Error receiving packet!" << std::endl;
- }
- av_packet_free(&pkt);
- av_freep(&frame->data[0]);
- av_frame_free(&frame);
- sws_freeContext(swsCtx);
- avcodec_free_context(&codecCtx);
- return compressedData;
- }
- int main() {
- int width, height;
- std::vector<uint8_t> screenshot = captureScreenshot(width, height);
- if (screenshot.empty()) {
- std::cerr << "Failed to capture screenshot!" << std::endl;
- return -1;
- }
- std::vector<uint8_t> compressedData = compressWithH264(screenshot, width, height);
- if (compressedData.empty()) {
- std::cerr << "Failed to compress screenshot!" << std::endl;
- return -1;
- }
- std::cout << "Screenshot captured and compressed. Size: " << compressedData.size() << " bytes." << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement