English 中文(简体)
为什么用这部FFmpeg法制作一系列图像的录像?
原标题:Why doesn t this FFmpeg code create a video from a series of images?

我成功地汇编了FFmpeg图书馆,供在SOS申请中使用。 我愿利用这一录像,从一系列图像中播放一个录像,但我似乎无法这样做。

The following is the code that 我请我使用:

AVCodec *codec;
AVCodecContext *c= NULL;
int i, out_size, size, outbuf_size;
FILE *f;
AVFrame *picture;
uint8_t *outbuf;

printf("Video encoding
");

/// find the mpeg video encoder
codec=avcodec_find_encoder(CODEC_ID_MPEG4);
//codec = avcodec_find_encoder(CODEC_ID_MPEG4);
if (!codec) {
    fprintf(stderr, "codec not found
");
    exit(1);
}

c= avcodec_alloc_context();
picture= avcodec_alloc_frame();

// put sample parameters 
c->bit_rate = 400000;
/// resolution must be a multiple of two 
c->width = 320;
c->height = 480;
//frames per second 
c->time_base= (AVRational){1,25};
c->gop_size = 10; /// emit one intra frame every ten frames
c->max_b_frames=1;
c->pix_fmt = PIX_FMT_YUV420P;

//open it
if (avcodec_open(c, codec) < 0) {
    fprintf(stderr, "could not open codec
");
    exit(1);
}

f = fopen([[NSTemporaryDirectory() stringByAppendingPathComponent:filename] UTF8String], "w");
if (!f) {
    fprintf(stderr, "could not open %s
",[filename UTF8String]);
    exit(1);
}

// alloc image and output buffer
outbuf_size = 100000;
outbuf = malloc(outbuf_size);
size = c->width * c->height;

#pragma mark -
AVFrame* outpic = avcodec_alloc_frame();
int nbytes = avpicture_get_size(PIX_FMT_YUV420P, c->width, c->height);

//create buffer for the output image
uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes);

#pragma mark -  
for(i=1;i<48;i++) {
    fflush(stdout);

    int numBytes = avpicture_get_size(PIX_FMT_YUV420P, c->width, c->height);
    uint8_t *buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]];
    CGImageRef newCgImage = [image CGImage];

    CGDataProviderRef dataProvider = CGImageGetDataProvider(newCgImage);
    CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
    buffer = (uint8_t *)CFDataGetBytePtr(bitmapData);   

    avpicture_fill((AVPicture*)picture, buffer, PIX_FMT_RGB24, c->width, c->height);
    avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, c->width, c->height);

    struct SwsContext* fooContext = sws_getContext(c->width, c->height, 
                                                   PIX_FMT_RGB24, 
                                                   c->width, c->height, 
                                                   PIX_FMT_YUV420P, 
                                                   SWS_FAST_BILINEAR, NULL, NULL, NULL);

    //perform the conversion
    sws_scale(fooContext, picture->data, picture->linesize, 0, c->height, outpic->data, outpic->linesize);
    // Here is where I try to convert to YUV

    // encode the image

    out_size = avcodec_encode_video(c, outbuf, outbuf_size, outpic);
    printf("encoding frame %3d (size=%5d)
", i, out_size);
    fwrite(outbuf, 1, out_size, f);

    free(buffer);
    buffer = NULL;      

}

// get the delayed frames
for(; out_size; i++) {
    fflush(stdout);

    out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
    printf("write frame %3d (size=%5d)
", i, out_size);
    fwrite(outbuf, 1, outbuf_size, f);      
}

// add sequence end code to have a real mpeg file
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0x01;
outbuf[3] = 0xb7;
fwrite(outbuf, 1, 4, f);
fclose(f);
free(outbuf);

avcodec_close(c);
av_free(c);
av_free(picture);
printf("
");

这部法典可能有什么错误?

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签