Advertisement
Guest User

Untitled

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