Skip to content

Instantly share code, notes, and snippets.

@habibg1232191
Created November 3, 2022 13:29
Show Gist options
  • Save habibg1232191/4e315d73eb9801816c002a6f342fd897 to your computer and use it in GitHub Desktop.
Save habibg1232191/4e315d73eb9801816c002a6f342fd897 to your computer and use it in GitHub Desktop.
#include <iostream>
extern "C" {
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
}
using namespace std;
const char* url = "<local url file>";
void log(const char *error_message) {
cout << error_message << endl;
}
int main() {
av_register_all();
avformat_network_init();
AVFormatContext *pFormatCtx = avformat_alloc_context();
if(avformat_open_input(&pFormatCtx, url, nullptr, nullptr) < 0){
log("avformat_open_input is Failed");
return -1;
}
if(avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
log("avformat_find_stream_info is Failed");
return -1;
}
int i;
AVCodecContext *pCodecCtxOrig = NULL;
AVCodecContext *pCodecCtx = NULL;
// Find the first video stream
int videoStream = -1;
AVCodecParameters *pCodecParameters = avcodec_parameters_alloc();
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream = i;
pCodecParameters = pFormatCtx->streams[i]->codecpar;
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
break;
}
if(videoStream == -1) {
log("Didn't find a video stream");
return -1;
}
AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
pCodecCtx = avcodec_alloc_context3(pCodec);
if(avcodec_parameters_to_context(pCodecCtx, pCodecParameters) < 0) {
fprintf(stderr, "Unable fill codec context!\n");
return -1;
}
if(pCodec==nullptr) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
if(avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) {
log("Could not open codec");
return -1;
}
AVFrame *pFrame = av_frame_alloc();
AVFrame *pFrameRGB = av_frame_alloc();
AVPacket *pPacket = av_packet_alloc();
SwsContext *imgConvertCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24,
SWS_BICUBIC, nullptr, nullptr, nullptr);
cout << pCodecCtx->width << "x" << pCodecCtx->height << endl;
int dst_fmt = AV_PIX_FMT_RGB24;
int dst_w = pCodecCtx->width;
int dst_h = pCodecCtx->height;
i = 0;
while (av_read_frame(pFormatCtx, pPacket) >= 0) {
pPacket = av_packet_alloc();
if (pPacket->stream_index == videoStream) {
int ret = avcodec_send_packet(pCodecCtx, pPacket);
// Get all the available frames from the decoder
while (ret >= 0) {
ret = avcodec_receive_frame(pCodecCtx, pFrame);
if (ret < 0) {
av_packet_free(&pPacket);
return -1;
}
av_frame_unref(pFrame);
}
cout << pFrame->height << endl;
AVFrame *pFrameRGB = av_frame_alloc();
//Allocate memory for the pixels of a picture and setup the AVPicture fields for it.
avpicture_alloc((AVPicture *) pFrameRGB, AV_PIX_FMT_RGB24, pFrame->width, pFrame->height);
if(i++ > 20) break;
}
av_packet_unref(pPacket);
}
avformat_close_input(&pFormatCtx);
av_packet_free(&pPacket);
av_frame_free(&pFrame);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment