使用FFmpeg样例代码在JNI Android上创建视频文件的静态图像

17

我如何修改以下FFMPEG示例代码,以创建一个视频文件,其中包含我在我的安卓手机上拥有的静态图像。我正在使用JNI来调用FFMPEG。

JNIEXPORT void JNICALL videoEncodeExample((JNIEnv *pEnv, jobject pObj, jstring filename)

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

     printf("Video encoding\n");

     /* find the mpeg1 video encoder */
     codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
     if (!codec) {
         fprintf(stderr, "codec not found\n");
         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 = 352;
     c->height = 288;
     /* 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\n");
         exit(1);
     }

     f = fopen(filename, "wb");
     if (!f) {
         fprintf(stderr, "could not open %s\n", filename);
         exit(1);
     }

     /* alloc image and output buffer */
     outbuf_size = 100000;
     outbuf = malloc(outbuf_size);
     size = c->width * c->height;
     picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */

     picture->data[0] = picture_buf;
     picture->data[1] = picture->data[0] + size;
     picture->data[2] = picture->data[1] + size / 4;
     picture->linesize[0] = c->width;
     picture->linesize[1] = c->width / 2;
     picture->linesize[2] = c->width / 2;

     /* encode 1 second of video */
     for(i=0;i<25;i++) {
         fflush(stdout);
         /* prepare a dummy image */
         /* Y */
         for(y=0;y<c->height;y++) {
             for(x=0;x<c->width;x++) {
                 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
             }
         }

         /* Cb and Cr */
         for(y=0;y<c->height/2;y++) {
             for(x=0;x<c->width/2;x++) {
                 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
                 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
             }
         }

         /* encode the image */
         out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
         printf("encoding frame %3d (size=%5d)\n", i, out_size);
         fwrite(outbuf, 1, out_size, f);
     }

     /* 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)\n", i, out_size);
         fwrite(outbuf, 1, out_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(picture_buf);
     free(outbuf);

     avcodec_close(c);
     av_free(c);
     av_free(picture);
     printf("\n");
    }

感谢和问候 安尼什


1
你好@anish,你找到解决问题的方法了吗?如果你找到了,请告诉我你是如何解决的。因为我也遇到了同样的问题,需要紧急解决方案...请帮帮我,谢谢 :) - MKJParekh
7
如果你提出更具体的问题,那么你会更有好运并得到更多回应。你已经粘贴了100行代码,但没有提到你尝试过什么或者什么具体的地方没有按计划工作。在此之前,我唯一能给出的答案是“非常小心地修改代码,直到它达到你期望的效果”。 - Error 454
这段代码我认为仅适用于一个图像,如何修改才能使用多个位图?我尝试复制FFmpeg可执行文件和lib到/system/bin和/system/lib,但不起作用,显示分段错误。 - Aashish Bhatnagar
嗨,Anish,你得到这个答案了吗? - rams
嗨,Anish,你解决问题了吗?如果是的话,能否请你分享一下你的输出? - Ravi K. Sharma
显示剩余2条评论
2个回答

1
在您的示例代码中,编码图像是虚拟的。 因此,我认为您应该将虚拟图像替换为Android设备中的实际图像,并记得将其格式转换为YUV420。

1
在您的代码中,您使用的图像格式为PIX_FMT_YUV420P。由于Android使用原始图片格式YUV420SP(PIX_FMT_NV21),因此您需要将图像转换为YUV420P格式,同时仍需使用ffmpeg提供的libswscale库对图像进行缩放处理。也许我的一个答案可以帮到您,请查看将YUV420SP转换为YUV420P

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接