Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
FFmpeg : avcodec_encode_video() and JPEG images
2 février 2012, par user105909I'm trying to encode a series of .jpg files into a video using the ffmpeg library, and I can't seem to get the frames to encode. (I have to use the ffmpeg library, and using ffmpeg from a command line is not an option in my case.)
Except for the part where I'm trying to open JPG files as AVFrames, my code is more or less the same thing as found in api-example.c from the ffmpeg library. When I populate the frames as the example does, everything works as expected. In the code below, I fail to encode any frames. Obviously the trouble is related to how I'm opening the JPG files, but I can't figure out what.
I'm opening the image like this:
AVFrame* open_image(const char* imageFileName, int width, int height, long * bufSize) { AVFormatContext *pFormatCtx; if(av_open_input_file(&pFormatCtx, imageFileName, NULL, 0, NULL)!=0) { printf("Can't open image file '%s'\n", imageFileName); return NULL; } AVCodecContext *pCodecCtx; 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\n"); return NULL; } // Open codec if(avcodec_open(pCodecCtx, pCodec)<0) { printf("Could not open codec\n"); return NULL; } AVFrame *pFrame = avcodec_alloc_frame(); if (!pFrame) { LOGV(TAG, "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) { sprintf(buf, "Frame is decoded, size %d", ret); LOGV(TAG, buf); pFrame->quality = 4; return pFrame; } else { // printf("Error [%d] while decoding frame: %s\n", ret, strerror(AVERROR(ret))); sprintf(buf, "Error %d decoding frame: %s", ret, strerror(AVERROR(ret))); LOGV(TAG, buf); } } }
...and attempting to encode them like this:
DIR * dir = opendir(path); int i = 0; if (dir != NULL) { for(struct dirent *ent = readdir(dir); ent != NULL; ent = readdir(dir)) { fflush(stdout); printf("%s/%s", path, ent->d_name); LOGV(TAG, filename); // If not a jpg file, pass it over const char * ext = strrchr(filename, '.'); if((!ext) || (strcmp(ext, ".jpg"))) { continue; } /*** NOTE: Is this where I've gone wrong? Bufsize is set in open_image based on av_picture_size() */ long bufSize = 0L; AVFrame * frame = open_image(filename, width, height, &bufSize); if(frame) { // This is what it needs to do, and it does not work. // Causes: // Wrong format? // Wrong buffer size? uint8_t * picBuf = (uint8_t *)malloc(bufSize); out_size = avcodec_encode_video(c, picBuf, bufSize, frame); printf("encoding frame %3d (size=%5d)\n", i++, out_size); /** On the first image, out_size is 0. On the next, it's -1, and fails. */ if(out_size < 0) { printf("Error encoding frame"); return -6; } fwrite(picBuf, 1, bufSize, f); free(picBuf); av_free(frame); } else { printf("Couldn't open image"); return -5; } } closedir(dir); } else { printf("Couldn't open directory %s\n", path); return -4; }
Could someone point me in the right direction?
-
How to Use FFMPEG in inside the Delphi [closed]
2 février 2012, par periyasamyIam beginner in delphi.i create the one sample application i need one help.how to use FFMPEG in inside the delphi?
-
reading video mpegts stream from stdin using libavformat libavcodec
1er février 2012, par D StarkweatherI'm trying to read video frames from stdin using ffmpeg's libav* lib collection. Simply passing "pipe:0" or "pipe:" as the filename to avformat_open_input_file() in my program does not do the trick. (e.g. cat /dev/video0 | ./myprogram OR ./myprogram < /dev/video0 ; it also fails using file.avi in place of /dev/video0).
Peeking into ffmpeg.c, I find the following bit of code using tcsetattr() to set the termios:
struct termios tty; if (tcgetattr (0, &tty) == 0) { oldtty = tty; restore_tty = 1; atexit(term_exit); tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |INLCR|IGNCR|ICRNL|IXON); tty.c_oflag |= OPOST; tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); tty.c_cflag &= ~(CSIZE|PARENB); tty.c_cflag |= CS8; tty.c_cc[VMIN] = 1; tty.c_cc[VTIME] = 0; tcsetattr (0, TCSANOW, &tty); } signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */ } avformat_network_deinit(); signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */ signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */ signal(SIGXCPU, sigterm_handler);
However, when I try this, it appears to have no effect. What settings do I need in the termios struct to continuously read video frames in ?
Currently, all i am able to do is call av_read_frames a few times (return code 0) before it usually either segfaults or hangs waiting. The buffer usually contains just a blank screen (all black). With any luck, the video buffer will contain the first video frame it comes across, but does not update to the later frames.
(I'm running this on Debian 6.0) Here's my version of ffmpeg:
ffmpeg version N-36936-g4cf81d9 Copyright (c) 2000-2012 the FFmpeg developers built on Jan 19 2012 20:51:47 with gcc 4.4.5
configuration: --enable-shared --enable-gray --enable-hardcoded-tables --enable-runtime-cpudetect --enable-libmp3lame --enable-libopenjpeg --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-zlib --enable-gpl libavutil 51. 34.101 / 51. 34.101 libavcodec 53. 57.100 / 53. 57.100 libavformat 53. 30.100 / 53. 30.100 libavdevice 53. 4.100 / 53. 4.100 libavfilter 2. 59.101 / 2. 59.101 libswscale 2. 1.100 / 2. 1.100 libswresample 0. 6.100 / 0. 6.100 libpostproc 52. 0.100 / 52. 0.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile].Thank you very much for any help helpful tips. It is much appreciated.
D. Grant Starkweather
-
linux vs windows in ffmpeg conversion
1er février 2012, par user1183169I am trying to convert flv files to mp3. Requirement is windows platform.
so whenever I execute the command in php code it results in failure.
I have tried to find how to use this command in windows but failed.
here is the command
ffmpeg -i /video.flv -vn -acodec copy /video.mp3
now i know the above command is linux based. I only want to know how to implement it in a windows environement.
THx in advance
-
'avcodec_decode_video' of ffmpeg doesn't work
1er février 2012, par sirupai use vc++ express, and am going to get with ffmpeg..
but with the 1st program i met a trouble.
vc++ says 'identifier 'avcodec_decode_video': identifier not found' on commpile process.
i don't know why....
next is waht i coded... .
include "avcodec.h" include "avformat.h" include "swscale.h" int main(int argc, char *argv[]) { av_register_all(); AVFormatContext *pFormatCtx; // Open video file if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0) return -1; // Couldn't open file // Retrieve stream information if(av_find_stream_info(pFormatCtx)<0) return -1; // Couldn't find stream information // Dump information about file onto standard error dump_format(pFormatCtx, 0, argv[1], 0); int i; AVCodecContext *pCodecCtx; // Find the first video stream int videoStream=-1; for(i=0; inb_streams; i++) if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) { videoStream=i; break; } if(videoStream==-1) return -1; // Didn't find a video stream // Get a pointer to the codec context for the video stream pCodecCtx=pFormatCtx->streams[videoStream]->codec; AVCodec *pCodec; // Find the decoder for the video stream pCodec=avcodec_find_decoder(pCodecCtx->codec_id); if(pCodec==NULL) { fprintf(stderr, "Unsupported codec!\n"); return -1; // Codec not found } // Open codec if(avcodec_open(pCodecCtx, pCodec)<0) return -1; // Could not open codec AVFrame *pFrame; // Allocate video frame pFrame=avcodec_alloc_frame(); // Allocate an AVFrame structure AVFrame* pFrameRGB=avcodec_alloc_frame(); if(pFrameRGB==NULL) return -1; uint8_t *buffer; int numBytes; // Determine required buffer size and allocate buffer numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,pCodecCtx->height); buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); // Assign appropriate parts of buffer to image planes in pFrameRGB // Note that pFrameRGB is an AVFrame, but AVFrame is a superset // of AVPicture avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height); int frameFinished; AVPacket packet; i=0; while(av_read_frame(pFormatCtx, &packet)>=0) { if(packet.stream_index==videoStream) { **// here makes compile error** avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size); if(frameFinished) { img_convert((AVPicture *)pFram eRGB, PIX_FMT_RGB24, (AVPicture*)pFrame, pCodecCtx->pix_fmt,pCodecCtx->width, pCodecCtx->height); } av_free_packet(&packet); } av_free(buffer); av_free(pFrameRGB); av_free(pFrame); avcodec_close(pCodecCtx); av_close_input_file(pFormatCtx); return 0;
}