Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.35 KB | None | 0 0
  1. AVPacket packet;
  2.     av_init_packet(&packet);
  3.     packet.data = NULL;
  4.     packet.size = 0;
  5.     int got_frame;
  6.     uint64_t input_audio_samples = 0;
  7.     fprintf(stderr, "transcoding....\n");
  8.  
  9.     while (av_read_frame(input_format_context, &packet) >= 0) {
  10.         // AVRational output_packet_timebase;
  11.         AVPacket output_packet;
  12.         output_packet.data = NULL;
  13.         output_packet.size = 0;
  14.         if (!frame) {
  15.             if (!(frame = av_frame_alloc())) {
  16.                 fprintf(stderr, "Could not allocate audio frame\n");
  17.                 goto exit;
  18.             }
  19.         }
  20.         int got_packet = 0;
  21.         av_init_packet(&output_packet);
  22.         if (packet.stream_index == in_video_index && packet.size > 0) {
  23.             fprintf(stderr, "Processing video frame index %d", in_video_index);
  24.             ret = avcodec_decode_video2(input_video_codec_context, frame, &got_frame, &packet);
  25.             if (ret < 0) {
  26.                 fprintf(stderr, "cannot open video codec");
  27.                 goto exit;
  28.             }
  29.             if (got_frame) {
  30.                 // encode
  31.                 //                video_frame = frame;
  32.                 fprintf(stderr, "we got video frame\n");
  33.                 ret = avcodec_encode_video2(output_video_codec_context, &output_packet, frame, &got_packet);
  34.                 if (ret >= 0 && got_packet) {
  35.                     // TODO: khong biet dung ko
  36.                     if (output_video_codec_context->coded_frame->pts != (signed) AV_NOPTS_VALUE) {
  37.                         output_packet.pts = av_rescale_q(output_video_stream->codec->coded_frame->pts,
  38.                                 output_video_stream->codec->time_base, output_video_stream->time_base);
  39.                     }
  40.                     // TODO: khong biet dung ko
  41.                     if (output_packet.dts == (signed) AV_NOPTS_VALUE) {
  42.                         output_packet.dts = output_packet.pts;
  43.                     } else {
  44.                         output_packet.dts = av_rescale_q(output_packet.dts,
  45.                                 output_video_stream->codec->time_base,
  46.                                 output_video_stream->time_base);
  47.                     }
  48.                     if (output_video_stream->codec->coded_frame->key_frame) {
  49.                         output_packet.flags |= AV_PKT_FLAG_KEY;
  50.                     }
  51.                     output_packet.stream_index = in_video_index;
  52.                     //output_packet_timebase = output_video_stream->time_base;
  53.                 } else {
  54.                     fprintf(stderr, "cannot encode video frame\n");
  55.                 }
  56.             }
  57.         } else if ((packet.stream_index == in_audio_index) && (packet.size > 0)) {
  58.             fprintf(stderr, "Processing audio frame index %d, bitrate %d, frame type: %d\n", in_audio_index, input_audio_stream->codec->bit_rate, frame->key_frame);
  59.             ret = avcodec_decode_audio4(input_audio_codec_context, frame, &got_frame, &packet);
  60.             if (ret < 0) {
  61.                 goto exit;
  62.                 fprintf(stderr, "Cannot decode audio stream\n");
  63.             }
  64.             if (got_frame) {
  65.                 fprintf(stderr, "we got audio frame\n");
  66.                 input_audio_samples += frame->nb_samples;
  67.                 ret = avcodec_encode_audio2(output_audio_codec_context, &output_packet, frame, &got_packet);
  68.                 if (ret >= 0 && got_packet) {
  69.                     output_packet.stream_index = in_audio_index;
  70.                     //output_packet_timebase = output_audio_stream->time_base;
  71.                 } else {
  72.                     fprintf(stderr, "Cannot encode audio frame\n");
  73.                     goto exit;
  74.                 }
  75.             }
  76.         }
  77.         if (got_packet && output_packet.data && (output_packet.size > 0)) {
  78.             ret = av_interleaved_write_frame(output_format_context, &output_packet);
  79.             if (ret < 0) {
  80.                 av_free_packet(&packet);
  81.                 av_free_packet(&output_packet);
  82.                 fprintf(stderr, "Cannot finalize writing packet\n");
  83.                 goto exit;
  84.             }
  85.         }
  86.         av_frame_free(&frame);
  87.         av_free_packet(&packet);
  88.         av_free_packet(&output_packet);
  89.     }
  90.     avio_flush(output_format_context->pb);
  91.     //avio_close(output_format_context->pb);
  92.     av_write_trailer(output_format_context);
  93.     av_free(io_context);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement