예제 #1
0
        int main(int argc, char **argv)
        {
            int      ret;
            AVPacket packet; //= { .data = NULL, .size = 0 };

            packet.data = null;
            packet.size = 0;
            AVFrame *frame = null;

            AVMediaType type;
            int         stream_index;
            int         i;
            int         got_frame;

            ffmpeg.av_register_all();
            ffmpeg.avfilter_register_all();
            if ((ret = OpenInputFile("test.flv")) < 0)
            {
                goto end;
            }
            if ((ret = OpenOutputFile("test.m3u8")) < 0)
            {
                goto end;
            }
            aacbsfc = ffmpeg.av_bitstream_filter_init("aac_adtstoasc");
            /* read all packets */
            int count = 0;
            int flag  = 1;

            while (true)
            {
                if ((ret = ffmpeg.av_read_frame(ifmt_ctx, &packet)) < 0)
                {
                    break;
                }
                stream_index = packet.stream_index;
                type         = ifmt_ctx->streams[packet.stream_index]->codec->codec_type;
                ffmpeg.av_log(null, ffmpeg.AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n");

                ffmpeg.av_log(null, ffmpeg.AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
                frame = ffmpeg.av_frame_alloc();
                if (null != frame)
                {
                    ret = ffmpeg.AVERROR(12);
                    break;
                }
                ffmpeg.av_packet_rescale_ts(&packet,
                                            ifmt_ctx->streams[stream_index]->time_base,
                                            ifmt_ctx->streams[stream_index]->codec->time_base);

                ret = dec_func(type, ifmt_ctx->streams[stream_index]->codec, frame, &got_frame, &packet);
                if (ret < 0)
                {
                    ffmpeg.av_frame_free(&frame);
                    ffmpeg.av_log(null, ffmpeg.AV_LOG_ERROR, "Decoding failed\n");
                    break;
                }
                if (got_frame == 0)
                {
                    frame->pts = frame->pkt_pts;
                    // frame->pts = av_frame_get_best_effort_timestamp(frame);
                    // frame->pts=count;
                    if (type == AVMediaType.AVMEDIA_TYPE_VIDEO)
                    {
                        ret = encode_write_frame(frame, stream_index, null);
                    }
                    else
                    {
                        if (flag != 0)
                        {
                            InitSwr(stream_index);
                            flag = 0;
                        }

                        AVFrame *frame_out = ffmpeg.av_frame_alloc();
                        if (0 != TransSample(frame, frame_out, stream_index))
                        {
                            ffmpeg.av_log(null, ffmpeg.AV_LOG_ERROR, "convert audio failed\n");
                            ret = -1;
                        }
                        // frame_out->pts = frame->pkt_pts;
                        ret = encode_write_frame(frame_out, stream_index, null);
                        ffmpeg.av_frame_free(&frame_out);
                    }
                    ffmpeg.av_frame_free(&frame);
                    if (ret < 0)
                    {
                        goto end;
                    }
                }
                else
                {
                    ffmpeg.av_frame_free(&frame);
                }
                ffmpeg.av_free_packet(&packet);
                ++count;
            }
            /* flush  encoders */
            // for (i = 0; i < ifmt_ctx->nb_streams; i++) {
            // ret = flush_encoder(i);
            // if (ret < 0) {
            // av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
            // goto end;
            // }
            // }
            ffmpeg.av_log(null, ffmpeg.AV_LOG_ERROR, "Flushing encoder failed\n");
            ffmpeg.av_write_trailer(ofmt_ctx);
end:
            ffmpeg.av_free_packet(&packet);
            ffmpeg.av_frame_free(&frame);
            ffmpeg.av_bitstream_filter_close(aacbsfc);
            for (i = 0; i < ifmt_ctx->nb_streams; i++)
            {
                ffmpeg.avcodec_close(ifmt_ctx->streams[i]->codec);
                if (ofmt_ctx != null && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] != null && ofmt_ctx->streams[i]->codec != null)
                {
                    ffmpeg.avcodec_close(ofmt_ctx->streams[i]->codec);
                }
            }
            // av_free(filter_ctx);
            fixed(AVFormatContext **ss = &ifmt_ctx)
            ffmpeg.avformat_close_input(ss);

            if (ofmt_ctx != null && (ofmt_ctx->oformat->flags & ffmpeg.AVFMT_NOFILE) == 0)
            {
                ffmpeg.avio_closep(&ofmt_ctx->pb);
            }
            ffmpeg.avformat_free_context(ofmt_ctx);

            // if (ret < 0)
            // av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret)); //av_err2str(ret));
            return(ret <= 0 ? 1 : 0);
        }
예제 #2
0
 public static extern int av_apply_bitstream_filters(AVCodecContext *codec, AVPacket *pkt, AVBitStreamFilterContext *bsfc);