[Из песочницы] FFmpeg начало работы с Visual Studio

ildshvj4y94npstenmkw5yle_xg.jpeg
// 21 апреля 2019
// Данный пример, немного исправленный, взят с http://dranger.com/ffmpeg/tutorial01.html
//
#include "pch.h"

extern "C"
{
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
}

#include 
#include 
#include 

#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)

// compatibility with newer API
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc avcodec_alloc_frame
#define av_frame_free avcodec_free_frame
#endif

void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
	FILE *pFile;
	char szFilename[32];
	int  y;

	// Создаем или открываес файл для записи изображения
	sprintf(szFilename, "frame%d.ppm", iFrame);
	pFile = fopen(szFilename, "wb");
	if (pFile == NULL)
		return;

	// Записуем заголовок файла
	fprintf(pFile, "P6\n%d %d\n255\n", width, height);

	// Записуем пиксельные данные
	for (y = 0; y < height; y++)
		fwrite(pFrame->data[0] + y * pFrame->linesize[0], 1, width * 3, pFile);

	// Закрываем файл
	fclose(pFile);
}

int main(int argc, char *argv[]) {
	AVFormatContext   *pFormatCtx = NULL;
	int               i, videoStream;
	AVCodecContext    *pCodecCtxOrig = NULL;
	AVCodecContext    *pCodecCtx = NULL;
	AVCodec           *pCodec = NULL;
	AVFrame           *pFrame = NULL;
	AVFrame           *pFrameRGB = NULL;
	AVPacket          packet;
	int               frameFinished;
	int               numBytes;
	uint8_t           *buffer = NULL;
	struct SwsContext *sws_ctx = NULL;

	if (argc < 2) {
		printf("Please provide a movie file\n");
		return -1;

	}
	// Регистрируем все форматы и кодеки
	av_register_all();

	// Пробуем открыть видео файл
	if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0)
		return -1; // Не могу открыть файл

				   // Пробуем получить информацию о потоке
	if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
		return -1; 

				   // Получаем подробную информацию о файле: продолжительность, битрейд, контейнер и прочее
	av_dump_format(pFormatCtx, 0, argv[1], 0);

	// Находим первый кард
	videoStream = -1;
	for (i = 0; i < pFormatCtx->nb_streams; i++)
		if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
			videoStream = i;
			break;
		}
	if (videoStream == -1)
		return -1; // Не нашли

				   // Указатель куда будут сохраняться данные 
	pCodecCtxOrig = pFormatCtx->streams[videoStream]->codec;
	// Находим подходящий декодер для файла
	pCodec = avcodec_find_decoder(pCodecCtxOrig->codec_id);
	if (pCodec == NULL) {
		fprintf(stderr, "Unsupported codec!\n");
		return -1; // Декодер не найден
	}
	// Копируем контекст
	pCodecCtx = avcodec_alloc_context3(pCodec);
	if (avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
		fprintf(stderr, "Couldn't copy codec context");
		return -1; // Ошибка копирования
	}

	// Открываем кодек
	if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
		return -1; // Не смогли открыть кодек

				   // Здесь будет храниться кадр
	pFrame = av_frame_alloc();

	// Здесь храниться кадр преобразованный в RGB
	pFrameRGB = av_frame_alloc();
	if (pFrameRGB == NULL)
		return -1;

	// Определяем необходимый размер буфера и выделяем память
	numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width,
		pCodecCtx->height);
	buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
	//  Связуем кадр с вновь выделенным буфером.
	avpicture_fill((AVPicture *)pFrameRGB, buffer, AV_PIX_FMT_RGB24,
		pCodecCtx->width, pCodecCtx->height);

	// Инициализируем SWS context для программного преобразования полученного кадра в RGB
	sws_ctx = sws_getContext(pCodecCtx->width,
		pCodecCtx->height,
		pCodecCtx->pix_fmt,
		pCodecCtx->width,
		pCodecCtx->height,
		AV_PIX_FMT_RGB24,
		SWS_BILINEAR,
		NULL,
		NULL,
		NULL
	);


	// Читаем кадры и каждый 25 копируем на диск
	i = 0;
	while (av_read_frame(pFormatCtx, &packet) >= 0) {
		// Это пакет видео потока?
		if (packet.stream_index == videoStream) {
			// Декодируем видео кадр
			avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

			// Мы получили видео кадр?
			if (frameFinished) {
				// Преобразуем кадр в RGB
				sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
					pFrame->linesize, 0, pCodecCtx->height,
					pFrameRGB->data, pFrameRGB->linesize);

				// Сохраняем кадр на диск
				if (++i % 25 == 0) SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height,i);
					
			}
		}

		// Освобождаем пакет
		av_free_packet(&packet);
	}

	// Освобождение памяти и закрытие кодеков
	av_free(buffer);
	av_frame_free(&pFrameRGB);

	av_frame_free(&pFrame);

	avcodec_close(pCodecCtx);
	avcodec_close(pCodecCtxOrig);

	avformat_close_input(&pFormatCtx);

	return 0;
}


© Habrahabr.ru