Advertisement
Guest User

Untitled

a guest
Jun 30th, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. converting images to mp4 using ffmpeg on iphone
  2. av_register_all();
  3. printf("Video encodingn");
  4.  
  5. /// find the mpeg video encoder
  6. //codec=avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
  7. codec = avcodec_find_encoder(CODEC_ID_MPEG4);
  8.  
  9. if (!codec) {
  10. fprintf(stderr, "codec not foundn");
  11. exit(1);
  12. }
  13.  
  14. c = avcodec_alloc_context();
  15. picture = avcodec_alloc_frame();
  16.  
  17. // put sample parameters
  18. c->bit_rate = 400000;
  19. /// resolution must be a multiple of two
  20. c->width = 240;
  21. c->height = 320;
  22. //c->codec_id = fmt->video_codec;
  23. //frames per second
  24. c->time_base= (AVRational){1,25};
  25. c->gop_size = 10; /// emit one intra frame every ten frames
  26. c->max_b_frames=1;
  27. c->pix_fmt =PIX_FMT_YUV420P; // PIX_FMT_YUV420P
  28.  
  29. if (avcodec_open(c, codec) < 0) {
  30. fprintf(stderr, "could not open codecn");
  31. exit(1);
  32. }
  33.  
  34. f = fopen([[NSHomeDirectory() stringByAppendingPathComponent:@"test.mp4"] UTF8String], "wb");
  35.  
  36. if (!f) {
  37. fprintf(stderr, "could not open %sn",[@"test.mp4" UTF8String]);
  38. exit(1);
  39. }
  40.  
  41. // alloc image and output buffer
  42. outbuf_size = 100000;
  43. outbuf = malloc(outbuf_size);
  44. size = c->width * c->height;
  45.  
  46. #pragma mark -
  47.  
  48. AVFrame* outpic = avcodec_alloc_frame();
  49. int nbytes = avpicture_get_size(PIX_FMT_YUV420P, c->width, c->height); //this is half size of numbytes.
  50.  
  51. //create buffer for the output image
  52. uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes);
  53.  
  54. #pragma mark -
  55. for(k=0;k<1;k++) {
  56. for(i=0;i<25;i++) {
  57. fflush(stdout);
  58.  
  59. int numBytes = avpicture_get_size(PIX_FMT_RGBA, c->width, c->height);
  60. uint8_t *buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
  61.  
  62. UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i+1]];
  63. CGImageRef newCgImage = [image CGImage];
  64.  
  65. CGDataProviderRef dataProvider = CGImageGetDataProvider(newCgImage);
  66. CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
  67. buffer = (uint8_t *)CFDataGetBytePtr(bitmapData);
  68. ///////////////////////////
  69. //outbuffer=(uint8_t *)CFDataGetBytePtr(bitmapData);
  70. //////////////////////////
  71. avpicture_fill((AVPicture*)picture, buffer, PIX_FMT_RGBA, c->width, c->height);
  72. avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, c->width, c->height);//does not have image data.
  73.  
  74. struct SwsContext* fooContext = sws_getContext(c->width, c->height,
  75. PIX_FMT_RGBA,
  76. c->width, c->height,
  77. PIX_FMT_YUV420P,
  78. SWS_FAST_BILINEAR, NULL, NULL, NULL);
  79.  
  80. //perform the conversion
  81. sws_scale(fooContext, picture->data, picture->linesize, 0, c->height, outpic->data, outpic->linesize);
  82. // Here is where I try to convert to YUV
  83.  
  84. // encode the image
  85. out_size = avcodec_encode_video(c, outbuf, outbuf_size, outpic);
  86. printf("encoding frame %3d (size=%5d)n", i, out_size);
  87. fwrite(outbuf, 1, out_size, f);
  88.  
  89. free(buffer);
  90. buffer = NULL;
  91. }
  92.  
  93. // get the delayed frames
  94. for(; out_size; i++) {
  95. fflush(stdout);
  96.  
  97. out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
  98. printf("write frame %3d (size=%5d)n", i, out_size);
  99. fwrite(outbuf, 1, outbuf_size, f);
  100. }
  101. }
  102.  
  103. // add sequence end code to have a real mpeg file
  104. outbuf[0] = 0x00;
  105. outbuf[1] = 0x00;
  106. outbuf[2] = 0x01;
  107. outbuf[3] = 0xb7;
  108. fwrite(outbuf, 1, 4, f);
  109. fclose(f);
  110. free(picture_buf);
  111. free(outbuf);
  112.  
  113. avcodec_close(c);
  114. av_free(c);
  115. av_free(picture);
  116. //av_free(outpic);
  117. printf("n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement