#include <stdlib.h>
#include <stdio.h>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#pragma warning(disable : 4996)
int main(int argc, char* argv[])
{
AVFormatContext *formatContext = NULL;
AVCodecContext *video_codec_context = NULL, *audio_codec_context = NULL;
AVCodec *video_codec = NULL, *audio_codec = NULL;
AVFrame *frame = NULL;
AVPacket packet;
int video_stream = -1, audio_stream = -1;
uint8_t *buffer = NULL;
int frameFinished = 0;
av_register_all();
formatContext = avformat_alloc_context();
if (avformat_open_input(&formatContext, "1.mp4", NULL, NULL) != 0)
return -1;
if (avformat_find_stream_info(formatContext, NULL) < 0)
return -1;
av_dump_format(formatContext, 0, "1.mp4", 0);
for (int i = 0; i < formatContext->nb_streams; i++)
{
if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO && video_stream == -1)
video_stream = i;
if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO && audio_stream == -1)
audio_stream = i;
}
if (video_stream == -1 || audio_stream == -1)
return -1;
video_codec_context = formatContext->streams[video_stream]->codec;
AVCodecParameters * audio_codec_params = formatContext->streams[audio_stream]->codecpar;
video_codec = avcodec_find_decoder(video_codec_context->codec_id);
if (video_codec == NULL)
return -1;
audio_codec = avcodec_find_decoder(audio_codec_params->codec_id);
if (audio_codec == NULL)
return -1;
avcodec_parameters_to_context(audio_codec_context, audio_codec_params);
if (avcodec_open2(video_codec_context, video_codec, NULL) < 0)
return -1;
if (avcodec_open2(audio_codec_context, audio_codec, NULL) < 0)
return -1;
frame = av_frame_alloc();
while (av_read_frame(formatContext, &packet) >= 0) {
frameFinished = 0;
if (packet.stream_index == audio_stream) {
int res = avcodec_decode_audio4(audio_codec_context, frame, &frameFinished, &packet);
if (frameFinished) {
int data_size = av_samples_get_buffer_size(NULL, audio_codec_context->channels,
frame->nb_samples,
audio_codec_context->sample_fmt,
1);
buffer = frame->data[0];
}
}
av_free_packet(&packet);
}
av_free(frame);
avcodec_close(video_codec_context);
avcodec_close(audio_codec_context);
avformat_close_input(&formatContext);
getchar();
return 0;
}
Не знаю по какой причине codecpar = NULL. Если без codecpar делать (по аналогии с video_codec_context), то audio_codec_context->channels = 0 и данных в фрейме нет.
Параметры видео:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
creation_time : 2016-08-14 22:29:45
Duration: 00:03:21.80, start: 0.000000, bitrate: 1756 kb/s
Stream #0:0(und): Video: h264 (Main) [avc1 / 0x31637661]
yuv420p, 1280x720 [PAR 1:1 DAR 16:9], 1627 kb/s
25 fps, 90k tbn (default)
Metadata:
creation_time : 2016-08-14 22:29:45
handler_name : ISO Media file produced by Google Inc.
Stream #0:1(und): Audio: aac (LC) [mp4a / 0x6134706D]
44100 Hz, stereo, fltp, 125 kb/s (default)
Metadata:
creation_time : 2016-08-14 22:29:45
handler_name : ISO Media file produced by Google Inc.
Кофе для программистов: как напиток влияет на продуктивность кодеров?
Рекламные вывески: как привлечь внимание и увеличить продажи
Стратегії та тренди в SMM - Технології, що формують майбутнє сьогодні
Выделенный сервер, что это, для чего нужен и какие характеристики важны?
Современные решения для бизнеса: как облачные и виртуальные технологии меняют рынок
Добрый день! Не могу понять, как правильно реализовать MVC паттерн, вот пример кода:
У меня бот для вкМне нужно что бы при вводе команды test бот ответил пользователю "Работаю"