Advertisement
Guest User

Untitled

a guest
Sep 5th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. int main()
  2. {
  3.  
  4.     av_register_all();
  5.  
  6.  
  7.     AVFormatContext *ps = avformat_alloc_context();
  8.  
  9.     AVFormatContext *ps2 = avformat_alloc_context();
  10.     AVOutputFormat *oF = av_guess_format("mp4", NULL, "video/mp4");
  11.     avformat_alloc_output_context2(&ps2, oF, NULL, "vid2.mp4");
  12.  
  13.     if(avformat_open_input(&ps, "vid.mp4", NULL, NULL) != 0)
  14.     {
  15.         printf("Failed to open input file.\n");
  16.         return -1;
  17.     }
  18.  
  19.     avio_open(&ps2->pb, "vid2.mp4", AVIO_FLAG_WRITE);
  20.  
  21.     avformat_find_stream_info(ps, NULL);
  22.  
  23.    // AVStream *iStream;
  24.     //AVStream *oStream;
  25.  
  26.     AVCodecContext *pC, *p2C;
  27.     //AVCodec *encoder;
  28.  
  29.     AVStream *oStream = NULL;
  30.     AVStream *iStream = NULL;
  31.  
  32.     AVCodec *encoder = NULL;
  33.  
  34.     for(unsigned int i = 0; i < ps->nb_streams; i++)
  35.     {
  36.         oStream = avformat_new_stream(ps2, NULL);
  37.         iStream = ps->streams[i];
  38.  
  39.         pC = iStream->codec;
  40.         avcodec_parameters_copy(oStream->codecpar, iStream->codecpar);
  41.  
  42.         if(pC->codec_type == AVMEDIA_TYPE_VIDEO || pC->codec_type == AVMEDIA_TYPE_AUDIO)
  43.         {
  44.             encoder = avcodec_find_encoder(pC->codec_id);
  45.  
  46.             avcodec_parameters_to_context(p2C, iStream->codecpar);
  47.  
  48.             avcodec_open2(p2C, encoder, NULL);
  49.             avcodec_open2(pC, avcodec_find_decoder(pC->codec_id), NULL);
  50.  
  51.  
  52.         }
  53.     }
  54.  
  55.     avformat_write_header(ps2, NULL);
  56.  
  57.     printf("Fine\n");
  58.  
  59.     AVFrame *rawFrame = av_frame_alloc();
  60.  
  61.     AVPacket *pkts = av_packet_alloc();
  62.     av_init_packet(pkts);
  63.     AVPacket *pktr = av_packet_alloc();
  64.     av_init_packet(pktr);
  65.  
  66.  
  67.     while(av_read_frame(ps, pkts) == 0)
  68.     {
  69.         //decoding
  70.         int ret1 = avcodec_send_packet(pC, pkts);
  71.         int ret2 = avcodec_receive_frame(pC, rawFrame);
  72.  
  73.         //encoding
  74.         avcodec_send_frame(p2C, rawFrame);
  75.         avcodec_receive_packet(p2C, pktr);
  76.  
  77.         int ret = av_interleaved_write_frame(ps2, pktr);
  78.     }
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement