Created
September 13, 2014 12:04
-
-
Save anonymous/41155aa9d9a1ff933c99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ffm_console.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
extern "C" { | |
#include "ffmpeg/inttypes.h" | |
#include "ffmpeg/include/libavutil/error.h" | |
#include "ffmpeg/include/libavutil/samplefmt.h" | |
#include "ffmpeg/include/libavcodec/avcodec.h" | |
#include "ffmpeg/include/libavformat/avformat.h" | |
#include "ffmpeg/include/libavutil/file.h" | |
#include "ffmpeg/include/libavutil/frame.h" | |
#include "ffmpeg/include/libswresample/swresample.h" | |
#include "ffmpeg/include/libavutil/channel_layout.h" | |
} | |
AVDictionaryEntry *tag = NULL; | |
AVFormatContext *pFormatCtx; | |
AVOutputFormat *pOutputFormatCtx; | |
AVCodecContext *codecctx = NULL; | |
AVCodec *aCodec; | |
AVPacket avpkt; | |
FILE * f; | |
static SwrContext * swrctx = NULL; | |
AVFrame *decoded_frame = NULL; | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
int errcode; | |
int duration = 0; | |
int loop_point = 0; | |
int total_samples = 0x7fffffff; | |
int loop_samples = 0; | |
avcodec_register_all(); | |
av_register_all(); | |
const char *filename = "input.flv"; | |
if ((errcode = avformat_open_input(&pFormatCtx, filename, NULL, NULL)) < 0) | |
{ | |
char errDescr[4096]; | |
av_strerror(errcode, errDescr, 4096); | |
return -1; | |
} | |
// | |
// find audio stream | |
// | |
int audioStreamIdx = 0; | |
for (int i=0;i<pFormatCtx->nb_streams;i++) | |
{ | |
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) | |
{ | |
audioStreamIdx = i; | |
break; | |
} | |
} | |
av_init_packet(&avpkt); | |
av_read_frame(pFormatCtx, &avpkt); | |
int header_size = avpkt.pos; | |
int full_packet_size = avpkt.size; | |
avformat_find_stream_info(pFormatCtx,NULL); | |
codecctx = pFormatCtx->streams[audioStreamIdx]->codec; | |
double temp = (double)(pFormatCtx->duration / 1000.0) * (double)(codecctx->sample_rate / 1000.0); | |
total_samples = (int)temp; | |
aCodec = avcodec_find_decoder(pFormatCtx->streams[audioStreamIdx]->codec->codec_id); | |
if (aCodec == NULL) | |
return -1; | |
int res = avcodec_open2(codecctx, aCodec, NULL); | |
if (res) | |
return -1; | |
// ---------------------------------------------------------- | |
// decode part | |
uint8_t sample_buffer[0x10000*4]; | |
int samples = 0; | |
int framecount = 0; | |
int got_frame = 0; | |
uint8_t *out = sample_buffer; | |
FILE * outfile = fopen("output.raw", "wb"); | |
int channel_layout = av_get_default_channel_layout(codecctx->channels); | |
swrctx = swr_alloc_set_opts(swrctx, channel_layout, AV_SAMPLE_FMT_S16, codecctx->sample_rate, channel_layout, codecctx->sample_fmt, codecctx->sample_rate, 0, NULL); | |
if (!swrctx || swr_init(swrctx) < 0) | |
{ | |
avcodec_close(codecctx); | |
return -1; | |
} | |
int frame = 0; | |
int bytes_written = 0; | |
while (true) | |
{ | |
int samples = 0; | |
int consumed_data = 0; | |
if (avpkt.size == 0) | |
{ | |
frame = av_read_frame(pFormatCtx, &avpkt); | |
if (frame != 0) | |
{ | |
fclose(outfile); | |
swr_free(&swrctx); | |
return 0; | |
} | |
} | |
if (!decoded_frame) | |
{ | |
if (!(decoded_frame = av_frame_alloc())) | |
return -1; | |
} | |
got_frame = 0; | |
consumed_data = avcodec_decode_audio4(codecctx, decoded_frame, &got_frame, &avpkt); | |
if (consumed_data < 0) | |
{ | |
avpkt.size = 0; | |
continue; | |
} | |
int total_sample_space = 0; | |
if (got_frame) | |
{ | |
memset(sample_buffer,0,0x40000); | |
samples = swr_convert(swrctx, &out, decoded_frame->nb_samples, (const uint8_t**)decoded_frame->extended_data, decoded_frame->nb_samples); | |
total_sample_space = av_samples_get_buffer_size(NULL, codecctx->channels, decoded_frame->nb_samples,codecctx->sample_fmt,1); | |
fwrite(sample_buffer, 1, total_sample_space, outfile); | |
bytes_written += samples * (2*codecctx->channels); | |
} | |
avpkt.size -= consumed_data; | |
avpkt.data += consumed_data; | |
} | |
av_frame_unref(decoded_frame); | |
fclose(outfile); | |
swr_free(&swrctx); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment