【发布时间】:2011-12-10 03:25:39
【问题描述】:
我如何修改以下 FFMPEG 示例代码,以便从我的 android 手机中的静止图像创建视频文件。我正在使用 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");
}
感谢和问候 阿尼什
【问题讨论】:
-
你好@anish你找到解决问题的方法了吗..如果你找到了,请告诉我你是如何解决的..因为我陷入了同样的困境,需要紧急寻求解决方案……请帮我个忙..谢谢:)
-
如果您有更具体的问题,您会获得更好的运气和更多的回复。您已经粘贴了 100 行代码,但没有提及您尝试过的内容或没有按计划工作的特定区域。在那之前,我能给出的唯一答案是非常仔细地修改代码,直到它达到你的预期
-
您可能想查看stackoverflow.com/questions/4705830/ffmpeg-encoding-in-c,这看起来像一个类似的问题,但可能您正在处理的部分已经修复。
-
您的 Android 应用中的静止图像是什么形式的?它们是位图吗?字节缓冲区?无论哪种方式,您都可以将数据传递给 JNI 代码以供 ffmpeg 进行编码。如果没有这些信息,您的问题是不完整的。
-
我相信这段代码只适用于一张图片,如何修改它以使用多个位图?我尝试将 ffmpeg 可执行文件和 lib 复制到 /system/bin 和 /system/lib 不起作用,它显示分段错误
标签: android ffmpeg java-native-interface android-ndk