Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (format != NULL){
- fmt = av_find_input_format(format);
- res = avformat_open_input(&oc, url, fmt , NULL);
- } else {
- res = avformat_open_input(&oc, url, NULL , NULL);
- }
- if (res < 0) {
- fprintf(stderr,"[LIBAV] Unable to open input file/url: %s\n", url);
- return res;
- }
- res = avformat_find_stream_info(oc, NULL);
- if (res < 0) {
- fprintf(stderr,"[LIBAV] Unable to read stream info for file/url: %s\n", url);
- libavClose();
- return res;
- }
- res = av_find_best_stream(oc, AVMEDIA_TYPE_VIDEO, -1, -1, &video_decoder, 0);
- if (res < 0){
- fprintf(stderr,"[LIBAV] No video stream found\n");
- libavClose();
- return -1;
- }
- video_st_index = res;
- video_st = oc->streams[video_st_index];
- video_codec = video_st->codec;
- if((res = avcodec_open2(video_codec, video_decoder, NULL))<0) {
- char error[1024];
- av_strerror(res, error, sizeof(error));
- fprintf(stderr,"[LIBAV] Could not open video decoder: %s\n", error);
- libavClose();
- return -1;
- }
- // Fetch Audio stream and Decoder
- res = av_find_best_stream(oc, AVMEDIA_TYPE_AUDIO, -1, -1, &audio_decoder, 0);
- if (res < 0){
- fprintf(stderr,"[LIBAV] No audio stream found\n");
- libavClose();
- return -1;
- }
- audio_st_index = res;
- audio_st = oc->streams[audio_st_index];
- audio_codec = audio_st->codec;
- if((res = avcodec_open2(audio_codec, audio_decoder, NULL))<0) {
- char error[1024];
- av_strerror(res, error, sizeof(error));
- fprintf(stderr,"[LIBAV] Could not open audio decoder: %s\n", error);
- libavClose();
- return -1;
- }
- // Dump source information
- av_dump_format(oc,0,url,0);
- // Setup audio/video
- video_buf = avcodec_alloc_frame();
- audio_buf = (u_int8_t *) av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
- while ((res = av_read_frame(oc, &packet))>=0 ) {
- orig = packet;
- if (packet.stream_index == audio_st_index) {
- // Keep reading audio samples from the packet (may contain multiple)
- while (packet.size > 0) {
- int chunk = AVCODEC_MAX_AUDIO_FRAME_SIZE;
- int in_used = avcodec_decode_audio3(audio_codec, (int16_t *) ((u_int8_t *) audio_buf + (*audio_samples) * av_get_bytes_per_sample( audio_st->codec->sample_fmt) * audio_st->codec->channels ) , &chunk, &packet);
- if (in_used < 0) {
- //printf("Invalid Audio Frame\n");
- // Decoding error -- skip this frame.
- break;
- }
- audio_samples += (chunk / av_get_bytes_per_sample( audio_st->codec->sample_fmt)) / audio_st->codec->channels;
- packet.data += in_used;
- packet.size -= in_used;
- }
- // If audio_samples were written, then update pointer
- if (audio_samples > 0) {
- // audio_buf stores audio_samples number of samples
- sampleCount += audio_samples;
- }
- } else if (packet.stream_index == video_st_index) {
- if (packet.size > 0) {
- frameFinished = 0;
- int in_used = avcodec_decode_video2(video_codec,video_buf, &frameFinished, &packet);
- if (in_used < 0) {
- //printf("Invalid Video Frame\n");
- // Decoding error -- skip this frame.
- } else {
- if(frameFinished) {
- frameCount++;
- // video_frame stores the current frame
- }
- packet.data += in_used;
- packet.size -= in_used;
- }
- }
- }
- av_free_packet(&orig); // free the packet
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement