Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "libavformat/avformat.h"
- #include "libavcodec/avcodec.h"
- #include "libavutil/avutil.h"
- #include "libavutil/rational.h"
- #include <stdio.h>
- int main()
- {
- av_register_all();
- //av_log_set_level(-8);
- AVFormatContext *ps = avformat_alloc_context();
- AVFormatContext *ps2 = avformat_alloc_context();
- AVOutputFormat *oF = av_guess_format("mp4", NULL, "video/mp4");
- if(avformat_open_input(&ps, "vid.mp4", NULL, NULL) != 0)
- {
- printf("Failed to open input file.\n");
- return -1;
- }
- avformat_alloc_output_context2(&ps2, ps->oformat, NULL, "vid2.mp4");
- avio_open(&ps2->pb, "vid2.mp4", AVIO_FLAG_WRITE);
- avformat_find_stream_info(ps, NULL);
- // AVStream *iStream;
- //AVStream *oStream;
- AVCodecContext *pC = avcodec_alloc_context3(NULL), *p2C = avcodec_alloc_context3(NULL);
- //AVCodec *encoder;
- AVStream *oStream = NULL;
- AVStream *iStream = NULL;
- AVCodec *encoder = NULL;
- for(unsigned int i = 0; i < ps->nb_streams; i++)
- {
- printf("%d\n", i);
- oStream = avformat_new_stream(ps2, NULL);
- iStream = ps->streams[i];
- avcodec_parameters_copy(oStream->codecpar, iStream->codecpar);
- //pC = iStream->codec;
- //p2C = oStream->codec;
- if(pC->codec_type == AVMEDIA_TYPE_VIDEO || pC->codec_type == AVMEDIA_TYPE_AUDIO)
- {
- //p2C = avcodec_alloc_context3(pC->codec);
- encoder = avcodec_find_encoder(pC->codec_id);
- /*oStream->time_base = iStream->time_base;
- p2C->pix_fmt = AV_PIX_FMT_YUV420P;
- p2C->flags = CODEC_FLAG_GLOBAL_HEADER;
- p2C->width = iStream->codec->width;
- p2C->height = iStream->codec->height;
- p2C->time_base = (AVRational){1, iStream->codec->framerate.num};
- p2C->gop_size = iStream->codec->framerate.num;
- p2C->bit_rate = iStream->codec->bit_rate;
- p2C->codec_tag = pC->codec_tag;
- //oStream->pts = iStream->pts;*/
- AVCodecParameters *par = avcodec_parameters_alloc();
- avcodec_parameters_from_context(par, pC);
- avcodec_parameters_to_context(p2C, par);
- avcodec_open2(p2C, encoder, NULL);
- avcodec_open2(pC, avcodec_find_decoder(pC->codec_id), NULL);
- //p2C-> = pC->pts;
- }
- }
- printf("done\n");
- int ret = avformat_write_header(ps2, NULL);
- char err[200];
- av_make_error_string(err, 200, ret);
- printf("Write header %d: %s\n", ret, err);
- AVFrame *rawFrame = av_frame_alloc();
- AVPacket *pkts = av_packet_alloc();
- //av_init_packet(pkts);
- AVPacket *pktr = av_packet_alloc();
- //av_init_packet(pktr);
- while(av_read_frame(ps, pkts) == 0)
- {
- //decoding
- if(avcodec_send_packet(pC, pkts) == 0)
- {
- if(avcodec_receive_frame(pC, rawFrame) == 0)
- {
- //encoding
- if(avcodec_send_frame(p2C, rawFrame) == 0)
- {
- if(avcodec_receive_packet(p2C, pktr) == 0)
- {
- printf("Succ dec/enc\n");
- if(av_interleaved_write_frame(ps2, pktr) != 0)
- {
- printf("Failed to write packet\n");
- break;
- }
- printf("packet written\n");
- }
- }
- }
- }
- }
- av_write_trailer(ps2);
- printf("Fine\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement