3

编辑
我检查了执行,发现错误不是(还)在 swscale 点。我当前的问题是找不到 JPG 图像:
No such file or directoryavformat_open_input(&pFormatCtx, imageFileName, NULL, NULL);
你告诉我我需要注册任何东西之前,我可以告诉我已经注册了(我更新了下面的代码)。
我还添加了访问外部存储的 Android 权限(我认为它与 Android 无关,因为我已经可以写入图像所在的 /mnt/sdcard/)
END EDIT

我已经阅读了几个教程(包括从 SO 发布的几个教程,即http://dranger.com/ffmpeg/,如何为 Android 编译 ffmpeg...,通过 dolphin-player 源代码)。这是我所拥有的:
。为 android 编译 ffmpeg
。运行基本教程,使用 NDK 在我的 android 设备上创建虚拟视频
。能够使用上面的虚拟视频代码的修改版本和大量谷歌搜索从 Ubuntu 上的图像生成 MPEG2 视频
。在 Android 设备上运行新代码会提供绿屏视频(无论我编码多少帧,持续时间为 1 秒)

我在类似情况下看到另一篇关于 iPhone 的帖子,其中提到 ARM 处理器优化可能是罪魁祸首。我尝试了一些 ldextra-flags(-arch armv7-a 和类似的)都没有成功。

我在最后包含了加载图像的代码。Android 和 linux 有什么不同吗?我的 ffmpeg 构建是否不适合 Android 视频编码?

void copyFrame(AVCodecContext *destContext, AVFrame* dest,
            AVCodecContext *srcContext, AVFrame* source) {
struct SwsContext *swsContext;
swsContext = sws_getContext(srcContext->width, srcContext->height, srcContext->pix_fmt,
                destContext->width, destContext->height, destContext->pix_fmt,
                SWS_FAST_BILINEAR, NULL, NULL, NULL);
sws_scale(swsContext, source->data, source->linesize, 0, srcContext->height, dest->data, dest->linesize);
sws_freeContext(swsContext);
}

int loadFromFile(const char* imageFileName, AVFrame* realPicture, AVCodecContext* videoContext) {
AVFormatContext *pFormatCtx = NULL;
avcodec_register_all();
av_register_all();

int ret = avformat_open_input(&pFormatCtx, imageFileName, NULL, NULL);
if (ret != 0) {
    // ERROR hapening here
    // Can't open image file. Use strerror(AVERROR(ret))) for details
    return ERR_CANNOT_OPEN_IMAGE;
}

AVCodecContext *pCodecCtx;

pCodecCtx = pFormatCtx->streams[0]->codec;
pCodecCtx->width = W_VIDEO;
pCodecCtx->height = H_VIDEO;
pCodecCtx->pix_fmt = PIX_FMT_YUV420P;

// Find the decoder for the video stream
AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (!pCodec) {
    // Codec not found
    return ERR_CODEC_NOT_FOUND;
}

// Open codec
if (avcodec_open(pCodecCtx, pCodec) < 0) {
    // Could not open codec
    return ERR_CANNOT_OPEN_CODEC;
}

// 
AVFrame *pFrame;

pFrame = avcodec_alloc_frame();

if (!pFrame) {
    // Can't allocate memory for AVFrame
    return ERR_CANNOT_ALLOC_MEM;
}

int frameFinished;
int numBytes;

// Determine required buffer size and allocate buffer
numBytes = avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof (uint8_t));

avpicture_fill((AVPicture *) pFrame, buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
AVPacket packet;
int res = 0;
while (av_read_frame(pFormatCtx, &packet) >= 0) {
    if (packet.stream_index != 0)
        continue;

    ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
    if (ret > 0) {
        // now, load the useful info into realPicture
        copyFrame(videoContext, realPicture, pCodecCtx, pFrame);
        // Free the packet that was allocated by av_read_frame
        av_free_packet(&packet);
        return 0;
    } else {
        // Error decoding frame. Use strerror(AVERROR(ret))) for details
        res = ERR_DECODE_FRAME;
    }
}
av_free(pFrame);

// close codec
avcodec_close(pCodecCtx);

// Close the image file
av_close_input_file(pFormatCtx);

return res;
}

一些 ./config 选项:
--extra-cflags="-O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 -mfloat-abi=softfp -mfpu=vfp -marm -march=armv7-a -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE"

--extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib -lc -lm -ldl -llog"

--arch=armv7-a --enable-armv5te --enable-armv6 --enable-armvfp --enable-memalign-hack

4

2 回答 2

1

一些指向 github 'android-ffmpeg' 的链接...

https://github.com/halfninja/android-ffmpeg-x264

https://github.com/guardianproject/android-ffmpeg

https://github.com/rowntreerob/android-ffmpeg

使用单个图片或一组图片制作 mpeg 的 CLI 指令...

http://ffmpeg.org/faq.html#How-do-I-encode-single-pictures-into-movies_003f

因此,如果您需要从 jpg 输入输出 mpeg / mp4,那么它并不复杂到需要您深入了解 libavcodec 或任何地方的所有详细方法调用。

您可以使用 NDK 和工具链简单地将 ffmpeg 构建为 android 可执行文件,然后在有根手机上测试 CLI 表达式,然后,如果它们有效,请在您的真实 android 应用程序中使用包装器库来简单地调用 ffmpeg.main,如监护人中所述项目。

您必须绕开 ffmpeg.main exit() 的调用方式,但它是一个非常小的 tweek。

SO上有很多关于 android-ffmpeg 中的 CLI 类型调用的线程。

除非您真的需要低级的东西,否则我看不到在 android 实现中参与该代码的好处。

于 2012-10-18T03:15:37.397 回答
0

上面的代码实际上是有效的。我在其他设备(三星 Galaxy Tab 7)上试过,没问题。该问题可能是由于 cyanomodgen 或特定的 Android 版本造成的。

于 2012-10-31T01:50:04.200 回答