Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <curl/curl.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <libavformat/avformat.h>
- #include <libavcodec/avcodec.h>
- #include <libavutil/avutil.h>
- #include <sys/file.h>
- #define TRUE 1
- FILE * captura_actual;
- AVCodec * codec;
- AVCodecContext * contexto_del_codec;
- AVFormatContext * contexto_del_formato;
- AVFrame* open_image(char* imageFileName, int width, int height)
- {
- printf("prueba"); // <-- this never gets printed
- AVFormatContext *pFormatCtx;
- if(avformat_open_input(&pFormatCtx, imageFileName, NULL,NULL)!=0)
- {
- printf("Can't open image file '%s'\n", imageFileName);
- return NULL;
- }
- printf("despues de avformat_open_input");
- AVCodecContext *pCodecCtx = pFormatCtx->streams[0]->codec;
- pCodecCtx->width = width;
- pCodecCtx->height = height;
- pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
- // Find the decoder for the video stream
- AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
- if (!pCodec)
- {
- printf("Codec not found");
- return NULL;
- }
- // Open codec
- if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
- {
- printf("Could not open codec");
- return NULL;
- }
- AVFrame *pFrame = av_frame_alloc();
- if (!pFrame)
- {
- printf("Can't allocate memory for AVFrame\n");
- return NULL;
- }
- int frameFinished;
- int numBytes;
- // Determine required buffer size and allocate buffer
- numBytes = avpicture_get_size(PIX_FMT_YUVJ420P, pCodecCtx->width,
- pCodecCtx->height);
- // ***
- //*bufSize = numBytes;
- // ***
- uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
- avpicture_fill((AVPicture *) pFrame, buffer, PIX_FMT_YUVJ420P,
- pCodecCtx->width, pCodecCtx->height);
- // Read frame
- AVPacket packet;
- int framesNumber = 0;
- while (av_read_frame(pFormatCtx, &packet) >= 0)
- {
- if(packet.stream_index != 0)
- continue;
- int ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
- &packet);
- if (ret > 0)
- {
- pFrame->quality = 1;
- return pFrame;
- }
- else {
- printf("Error [%d] while decoding frame: %s\n", ret,
- strerror(AVERROR(ret)));
- }
- }
- }
- void obtenerImagen() {
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- int fdcaptura_actual = open("captura_actual.jpg",O_RDWR | O_CREAT, 0666);
- int resultado = flock(fdcaptura_actual, LOCK_EX | LOCK_NB);
- if (resultado) {
- printf("Fallo al obtener el acceso exclusivo");
- }
- captura_actual = fdopen(fdcaptura_actual,"w");
- if (captura_actual==NULL) return;
- if(curl) {
- curl_easy_setopt(curl, CURLOPT_URL, "http://viajesdonna.hopto.org:7777/tmpfs/auto.jpg");
- curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
- curl_easy_setopt(curl, CURLOPT_USERNAME, "admin");
- curl_easy_setopt(curl, CURLOPT_PASSWORD, "20donna15");
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, captura_actual);
- res = curl_easy_perform(curl);
- if(res != CURLE_OK) {
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
- }
- curl_easy_cleanup(curl);
- }
- fclose(captura_actual);
- // curl_easy_cleanup(curl);
- }
- void limpiarOperatoria() {
- av_write_trailer(contexto_del_formato);
- avcodec_close(contexto_del_codec);
- av_freep(contexto_del_codec);
- }
- void crearArchivo() {
- AVOutputFormat * formatoSalida = av_guess_format(NULL,"prueba.avi",NULL);
- if (formatoSalida == NULL) {
- formatoSalida = av_guess_format("mpeg",NULL,NULL);
- }
- codec = avcodec_find_encoder(formatoSalida->video_codec);
- contexto_del_codec = avcodec_alloc_context3(codec);
- contexto_del_codec->codec_id = formatoSalida->video_codec;
- contexto_del_codec->codec_type = AVMEDIA_TYPE_VIDEO;
- contexto_del_codec->gop_size = 30;
- contexto_del_codec->width = 640;
- contexto_del_codec->height = 352;
- contexto_del_codec->max_b_frames = 0;
- contexto_del_codec->bit_rate = 640 * 352 * 4;
- contexto_del_codec->pix_fmt = PIX_FMT_YUV420P;
- contexto_del_codec->time_base = (AVRational){24,1};
- contexto_del_codec->framerate = (AVRational){24,1};
- contexto_del_formato = avformat_alloc_context();
- contexto_del_formato->oformat = formatoSalida;
- contexto_del_formato->video_codec_id = formatoSalida->video_codec;
- snprintf(contexto_del_formato->filename, sizeof(contexto_del_formato->filename),"%s","prueba.avi");
- AVStream * flujo_de_video = avformat_new_stream(contexto_del_formato,codec);
- flujo_de_video->time_base = (AVRational){24,1};
- if (!flujo_de_video)
- {
- printf("Error al alocar memoria\n");
- }
- flujo_de_video->codec = contexto_del_codec;
- avcodec_open2(contexto_del_codec,codec,NULL);
- avio_open(&contexto_del_formato->pb,"prueba.avi",AVIO_FLAG_WRITE);
- avformat_write_header(contexto_del_formato,NULL);
- }
- void inicializacion() {
- avcodec_register_all();
- av_register_all();
- }
- int main(int argc, char ** argv) {
- printf("Inicio..\n");
- inicializacion();
- // if (access("prueba.avi", F_OK) != -1) crearArchivo();
- // while (TRUE) {
- // obtenerImagen();
- //agregarCuadro();
- // sleep(5);
- // }
- printf("antes\n");
- AVFrame * cuadro = open_image("captura_actual.jpg",640,352);
- printf("despues\n");
- limpiarOperatoria();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement