是的,你可以使用libstagefright,它非常强大。
由于stagefright未暴露给NDK,所以您需要做额外的工作。
有两种方法:
(1) 使用Android完整源代码构建您的项目。这种方式需要花费几天时间进行设置,一旦设置好了,就非常容易,并且您可以充分利用stagefright。
(2) 您可以将include文件夹复制到您的项目中,它位于此文件夹中:
android-4.0.4_r1.1/frameworks/base/include/media/stagefright
然后,您需要通过动态加载libstagefright.so来导出库函数,并将其与JNI项目链接。
使用statgefright进行编码/解码非常简单,只需要几百行即可完成。
我使用stagefright捕获屏幕截图创建视频,该视频将很快在我们的Android VNC服务器上发布。
以下是一段代码片段,我认为它比使用ffmpeg编码电影更好。您也可以添加音频源。
class ImageSource : public MediaSource {
ImageSource(int width, int height, int colorFormat)
: mWidth(width),
mHeight(height),
mColorFormat(colorFormat)
{
}
virtual status_t read(
MediaBuffer **buffer, const MediaSource::ReadOptions *options) {
}
...
};
int width = 720;
int height = 480;
sp<MediaSource> img_source = new ImageSource(width, height, colorFormat);
sp<MetaData> enc_meta = new MetaData;
enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
enc_meta->setInt32(kKeyWidth, width);
enc_meta->setInt32(kKeyHeight, height);
enc_meta->setInt32(kKeySampleRate, kFramerate);
enc_meta->setInt32(kKeyBitRate, kVideoBitRate);
enc_meta->setInt32(kKeyStride, width);
enc_meta->setInt32(kKeySliceHeight, height);
enc_meta->setInt32(kKeyIFramesInterval, kIFramesIntervalSec);
enc_meta->setInt32(kKeyColorFormat, colorFormat);
sp<MediaSource> encoder =
OMXCodec::Create(
client.interface(), enc_meta, true, image_source);
sp<MPEG4Writer> writer = new MPEG4Writer("/sdcard/screenshot.mp4");
writer->addSource(encoder);
writer->setMaxFileDuration(kDurationUs);
CHECK_EQ(OK, writer->start());
while (!writer->reachedEOS()) {
fprintf(stderr, ".");
usleep(100000);
}
err = writer->stop();