Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AVPacket packet;
- av_init_packet(&packet);
- packet.data = NULL;
- packet.size = 0;
- int got_frame;
- uint64_t input_audio_samples = 0;
- fprintf(stderr, "transcoding....\n");
- while (av_read_frame(input_format_context, &packet) >= 0) {
- // AVRational output_packet_timebase;
- AVPacket output_packet;
- output_packet.data = NULL;
- output_packet.size = 0;
- if (!frame) {
- if (!(frame = av_frame_alloc())) {
- fprintf(stderr, "Could not allocate audio frame\n");
- goto exit;
- }
- }
- int got_packet = 0;
- av_init_packet(&output_packet);
- if (packet.stream_index == in_video_index && packet.size > 0) {
- fprintf(stderr, "Processing video frame index %d", in_video_index);
- ret = avcodec_decode_video2(input_video_codec_context, frame, &got_frame, &packet);
- if (ret < 0) {
- fprintf(stderr, "cannot open video codec");
- goto exit;
- }
- if (got_frame) {
- // encode
- // video_frame = frame;
- fprintf(stderr, "we got video frame\n");
- ret = avcodec_encode_video2(output_video_codec_context, &output_packet, frame, &got_packet);
- if (ret >= 0 && got_packet) {
- // TODO: khong biet dung ko
- if (output_video_codec_context->coded_frame->pts != (signed) AV_NOPTS_VALUE) {
- output_packet.pts = av_rescale_q(output_video_stream->codec->coded_frame->pts,
- output_video_stream->codec->time_base, output_video_stream->time_base);
- }
- // TODO: khong biet dung ko
- if (output_packet.dts == (signed) AV_NOPTS_VALUE) {
- output_packet.dts = output_packet.pts;
- } else {
- output_packet.dts = av_rescale_q(output_packet.dts,
- output_video_stream->codec->time_base,
- output_video_stream->time_base);
- }
- if (output_video_stream->codec->coded_frame->key_frame) {
- output_packet.flags |= AV_PKT_FLAG_KEY;
- }
- output_packet.stream_index = in_video_index;
- //output_packet_timebase = output_video_stream->time_base;
- } else {
- fprintf(stderr, "cannot encode video frame\n");
- }
- }
- } else if ((packet.stream_index == in_audio_index) && (packet.size > 0)) {
- 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);
- ret = avcodec_decode_audio4(input_audio_codec_context, frame, &got_frame, &packet);
- if (ret < 0) {
- goto exit;
- fprintf(stderr, "Cannot decode audio stream\n");
- }
- if (got_frame) {
- fprintf(stderr, "we got audio frame\n");
- input_audio_samples += frame->nb_samples;
- ret = avcodec_encode_audio2(output_audio_codec_context, &output_packet, frame, &got_packet);
- if (ret >= 0 && got_packet) {
- output_packet.stream_index = in_audio_index;
- //output_packet_timebase = output_audio_stream->time_base;
- } else {
- fprintf(stderr, "Cannot encode audio frame\n");
- goto exit;
- }
- }
- }
- if (got_packet && output_packet.data && (output_packet.size > 0)) {
- ret = av_interleaved_write_frame(output_format_context, &output_packet);
- if (ret < 0) {
- av_free_packet(&packet);
- av_free_packet(&output_packet);
- fprintf(stderr, "Cannot finalize writing packet\n");
- goto exit;
- }
- }
- av_frame_free(&frame);
- av_free_packet(&packet);
- av_free_packet(&output_packet);
- }
- avio_flush(output_format_context->pb);
- //avio_close(output_format_context->pb);
- av_write_trailer(output_format_context);
- av_free(io_context);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement