Advertisement
Guest User

Untitled

a guest
Sep 7th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include "libavformat/avformat.h"
  2. #include "libavcodec/avcodec.h"
  3. #include "libavutil/avutil.h"
  4. #include "libavutil/rational.h"
  5.  
  6. #include <stdio.h>
  7.  
  8. int main()
  9. {
  10.  
  11.     av_register_all();
  12.  
  13.     //av_log_set_level(-8);
  14.  
  15.  
  16.     AVFormatContext *ps = avformat_alloc_context();
  17.  
  18.     AVFormatContext *ps2 = avformat_alloc_context();
  19.     AVOutputFormat *oF = av_guess_format("mp4", NULL, "video/mp4");
  20.  
  21.  
  22.     if(avformat_open_input(&ps, "vid.mp4", NULL, NULL) != 0)
  23.     {
  24.         printf("Failed to open input file.\n");
  25.         return -1;
  26.     }
  27.  
  28.     avformat_alloc_output_context2(&ps2, ps->oformat, NULL, "vid2.mp4");
  29.  
  30.     avio_open(&ps2->pb, "vid2.mp4", AVIO_FLAG_WRITE);
  31.  
  32.     avformat_find_stream_info(ps, NULL);
  33.  
  34.    // AVStream *iStream;
  35.     //AVStream *oStream;
  36.  
  37.     AVCodecContext *pC = avcodec_alloc_context3(NULL), *p2C = avcodec_alloc_context3(NULL);
  38.     //AVCodec *encoder;
  39.  
  40.     AVStream *oStream = NULL;
  41.     AVStream *iStream = NULL;
  42.  
  43.     AVCodec *encoder = NULL;
  44.  
  45.     for(unsigned int i = 0; i < ps->nb_streams; i++)
  46.     {
  47.         printf("%d\n", i);
  48.         oStream = avformat_new_stream(ps2, NULL);
  49.         iStream = ps->streams[i];
  50.  
  51.         avcodec_parameters_copy(oStream->codecpar, iStream->codecpar);
  52.         //pC = iStream->codec;
  53.         //p2C = oStream->codec;
  54.  
  55.  
  56.         if(pC->codec_type == AVMEDIA_TYPE_VIDEO || pC->codec_type == AVMEDIA_TYPE_AUDIO)
  57.         {
  58.             //p2C = avcodec_alloc_context3(pC->codec);
  59.             encoder = avcodec_find_encoder(pC->codec_id);
  60.  
  61.  
  62.  
  63.             /*oStream->time_base = iStream->time_base;
  64.             p2C->pix_fmt = AV_PIX_FMT_YUV420P;
  65.             p2C->flags = CODEC_FLAG_GLOBAL_HEADER;
  66.             p2C->width = iStream->codec->width;
  67.             p2C->height = iStream->codec->height;
  68.             p2C->time_base = (AVRational){1, iStream->codec->framerate.num};
  69.             p2C->gop_size = iStream->codec->framerate.num;
  70.             p2C->bit_rate = iStream->codec->bit_rate;
  71.             p2C->codec_tag = pC->codec_tag;
  72.             //oStream->pts = iStream->pts;*/
  73.  
  74.             AVCodecParameters *par = avcodec_parameters_alloc();
  75.             avcodec_parameters_from_context(par, pC);
  76.             avcodec_parameters_to_context(p2C, par);
  77.  
  78.             avcodec_open2(p2C, encoder, NULL);
  79.             avcodec_open2(pC, avcodec_find_decoder(pC->codec_id), NULL);
  80.  
  81.             //p2C-> = pC->pts;
  82.         }
  83.     }
  84.     printf("done\n");
  85.  
  86.     int ret = avformat_write_header(ps2, NULL);
  87.     char err[200];
  88.     av_make_error_string(err, 200, ret);
  89.     printf("Write header %d: %s\n", ret, err);
  90.  
  91.  
  92.  
  93.     AVFrame *rawFrame = av_frame_alloc();
  94.  
  95.     AVPacket *pkts = av_packet_alloc();
  96.     //av_init_packet(pkts);
  97.     AVPacket *pktr = av_packet_alloc();
  98.     //av_init_packet(pktr);
  99.  
  100.     while(av_read_frame(ps, pkts) == 0)
  101.     {
  102.         //decoding
  103.         if(avcodec_send_packet(pC, pkts) == 0)
  104.         {
  105.             if(avcodec_receive_frame(pC, rawFrame) == 0)
  106.             {
  107.             //encoding
  108.                 if(avcodec_send_frame(p2C, rawFrame) == 0)
  109.                 {
  110.                     if(avcodec_receive_packet(p2C, pktr) == 0)
  111.                     {
  112.                         printf("Succ dec/enc\n");
  113.                         if(av_interleaved_write_frame(ps2, pktr) != 0)
  114.                         {
  115.                             printf("Failed to write packet\n");
  116.                             break;
  117.                         }
  118.                         printf("packet written\n");
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.     }
  124.     av_write_trailer(ps2);
  125.     printf("Fine\n");
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement