Advertisement
Guest User

Untitled

a guest
May 29th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <curl/curl.h>
  6. #include <pthread.h>
  7. #include <unistd.h>
  8. #include <libavformat/avformat.h>
  9. #include <libavcodec/avcodec.h>
  10. #include <libavutil/avutil.h>
  11. #include <sys/file.h>
  12. #define TRUE 1
  13. FILE * captura_actual;
  14. AVCodec * codec;
  15. AVCodecContext * contexto_del_codec;
  16. AVFormatContext * contexto_del_formato;
  17. AVFrame* open_image(char* imageFileName, int width, int height)
  18. {
  19. printf("prueba"); // <-- this never gets printed
  20. AVFormatContext *pFormatCtx;
  21.  
  22. if(avformat_open_input(&pFormatCtx, imageFileName, NULL,NULL)!=0)
  23. {
  24. printf("Can't open image file '%s'\n", imageFileName);
  25. return NULL;
  26. }
  27. printf("despues de avformat_open_input");
  28. AVCodecContext *pCodecCtx = pFormatCtx->streams[0]->codec;
  29. pCodecCtx->width = width;
  30. pCodecCtx->height = height;
  31. pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
  32.  
  33. // Find the decoder for the video stream
  34. AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
  35. if (!pCodec)
  36. {
  37. printf("Codec not found");
  38. return NULL;
  39. }
  40.  
  41. // Open codec
  42. if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
  43. {
  44. printf("Could not open codec");
  45. return NULL;
  46. }
  47.  
  48. AVFrame *pFrame = av_frame_alloc();
  49. if (!pFrame)
  50. {
  51. printf("Can't allocate memory for AVFrame\n");
  52. return NULL;
  53. }
  54.  
  55. int frameFinished;
  56. int numBytes;
  57.  
  58. // Determine required buffer size and allocate buffer
  59. numBytes = avpicture_get_size(PIX_FMT_YUVJ420P, pCodecCtx->width,
  60. pCodecCtx->height);
  61.  
  62. // ***
  63. //*bufSize = numBytes;
  64. // ***
  65.  
  66. uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
  67.  
  68. avpicture_fill((AVPicture *) pFrame, buffer, PIX_FMT_YUVJ420P,
  69. pCodecCtx->width, pCodecCtx->height);
  70.  
  71. // Read frame
  72.  
  73. AVPacket packet;
  74.  
  75. int framesNumber = 0;
  76. while (av_read_frame(pFormatCtx, &packet) >= 0)
  77. {
  78. if(packet.stream_index != 0)
  79. continue;
  80.  
  81. int ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
  82. &packet);
  83. if (ret > 0)
  84. {
  85. pFrame->quality = 1;
  86. return pFrame;
  87. }
  88. else {
  89. printf("Error [%d] while decoding frame: %s\n", ret,
  90. strerror(AVERROR(ret)));
  91. }
  92. }
  93. }
  94. void obtenerImagen() {
  95. CURL *curl;
  96. CURLcode res;
  97. curl = curl_easy_init();
  98. int fdcaptura_actual = open("captura_actual.jpg",O_RDWR | O_CREAT, 0666);
  99.  
  100. int resultado = flock(fdcaptura_actual, LOCK_EX | LOCK_NB);
  101. if (resultado) {
  102. printf("Fallo al obtener el acceso exclusivo");
  103. }
  104. captura_actual = fdopen(fdcaptura_actual,"w");
  105. if (captura_actual==NULL) return;
  106. if(curl) {
  107. curl_easy_setopt(curl, CURLOPT_URL, "http://viajesdonna.hopto.org:7777/tmpfs/auto.jpg");
  108. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
  109. curl_easy_setopt(curl, CURLOPT_USERNAME, "admin");
  110. curl_easy_setopt(curl, CURLOPT_PASSWORD, "20donna15");
  111. curl_easy_setopt(curl, CURLOPT_WRITEDATA, captura_actual);
  112. res = curl_easy_perform(curl);
  113. if(res != CURLE_OK) {
  114. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  115. curl_easy_strerror(res));
  116. }
  117. curl_easy_cleanup(curl);
  118. }
  119. fclose(captura_actual);
  120. // curl_easy_cleanup(curl);
  121.  
  122. }
  123. void limpiarOperatoria() {
  124. av_write_trailer(contexto_del_formato);
  125. avcodec_close(contexto_del_codec);
  126. av_freep(contexto_del_codec);
  127. }
  128. void crearArchivo() {
  129. AVOutputFormat * formatoSalida = av_guess_format(NULL,"prueba.avi",NULL);
  130. if (formatoSalida == NULL) {
  131. formatoSalida = av_guess_format("mpeg",NULL,NULL);
  132. }
  133. codec = avcodec_find_encoder(formatoSalida->video_codec);
  134. contexto_del_codec = avcodec_alloc_context3(codec);
  135. contexto_del_codec->codec_id = formatoSalida->video_codec;
  136. contexto_del_codec->codec_type = AVMEDIA_TYPE_VIDEO;
  137. contexto_del_codec->gop_size = 30;
  138. contexto_del_codec->width = 640;
  139. contexto_del_codec->height = 352;
  140. contexto_del_codec->max_b_frames = 0;
  141. contexto_del_codec->bit_rate = 640 * 352 * 4;
  142. contexto_del_codec->pix_fmt = PIX_FMT_YUV420P;
  143. contexto_del_codec->time_base = (AVRational){24,1};
  144. contexto_del_codec->framerate = (AVRational){24,1};
  145. contexto_del_formato = avformat_alloc_context();
  146. contexto_del_formato->oformat = formatoSalida;
  147. contexto_del_formato->video_codec_id = formatoSalida->video_codec;
  148. snprintf(contexto_del_formato->filename, sizeof(contexto_del_formato->filename),"%s","prueba.avi");
  149. AVStream * flujo_de_video = avformat_new_stream(contexto_del_formato,codec);
  150. flujo_de_video->time_base = (AVRational){24,1};
  151. if (!flujo_de_video)
  152. {
  153. printf("Error al alocar memoria\n");
  154. }
  155. flujo_de_video->codec = contexto_del_codec;
  156. avcodec_open2(contexto_del_codec,codec,NULL);
  157. avio_open(&contexto_del_formato->pb,"prueba.avi",AVIO_FLAG_WRITE);
  158. avformat_write_header(contexto_del_formato,NULL);
  159. }
  160. void inicializacion() {
  161. avcodec_register_all();
  162. av_register_all();
  163. }
  164. int main(int argc, char ** argv) {
  165. printf("Inicio..\n");
  166. inicializacion();
  167. // if (access("prueba.avi", F_OK) != -1) crearArchivo();
  168. // while (TRUE) {
  169. // obtenerImagen();
  170. //agregarCuadro();
  171. // sleep(5);
  172. // }
  173.  
  174.  
  175. printf("antes\n");
  176. AVFrame * cuadro = open_image("captura_actual.jpg",640,352);
  177. printf("despues\n");
  178. limpiarOperatoria();
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement