android - FFMPEG 视频缩略图

标签 android ffmpeg android-ndk android-ffmpeg

我正在尝试使用 ffmpeg(v3.4) 从视频中提取缩略图以获取视频缩略图,此代码适用于大多数视频文件。

但在 sws_getContext 函数的某些视频上崩溃 请帮助。提前致谢。任何帮助将不胜感激。

const int TARGET_IMAGE_FORMAT = AV_PIX_FMT_RGBA;
    const int TARGET_IMAGE_CODEC = AV_CODEC_ID_PNG;

    int stream_component_open(State *s, int stream_index) 
{
        AVFormatContext *pFormatCtx = s->pFormatCtx;
        AVCodecContext *codecCtx;
        AVCodec *codec;
        if (stream_index < 0 || stream_index >= pFormatCtx->nb_streams) {
            return FAILURE;
        }
        codecCtx = pFormatCtx->streams[stream_index]->codec;
        codec = avcodec_find_decoder(codecCtx->codec_id);
        if (codec == NULL) {
            return FAILURE;
        }
        if (!codec || (avcodec_open2(codecCtx, codec, NULL) < 0)) {
            return FAILURE;
        }
        switch(codecCtx->codec_type) {

            case AVMEDIA_TYPE_VIDEO:
                s->video_stream = stream_index;
                s->video_st = pFormatCtx->streams[stream_index];
                AVCodec *targetCodec = avcodec_find_encoder(TARGET_IMAGE_CODEC);
                if (!targetCodec) {
                    return FAILURE;
                }
                s->codecCtx = avcodec_alloc_context3(targetCodec);
                if (!s->codecCtx) {
                    return FAILURE;
                }
                s->codecCtx->bit_rate = s->video_st->codec->bit_rate;
                s->codecCtx->width = s->video_st->codec->width;
                s->codecCtx->height = s->video_st->codec->height;
                s->codecCtx->pix_fmt = TARGET_IMAGE_FORMAT;
                s->codecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
                s->codecCtx->time_base.num = s->video_st->codec->time_base.num;
                s->codecCtx->time_base.den = s->video_st->codec->time_base.den;

                if (!targetCodec || avcodec_open2(s->codecCtx, targetCodec, NULL) < 0) {
                    return FAILURE;
                }
    __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "CRASH START");
                s->sws_ctx = sws_getContext(s->video_st->codec->width,
                        s->video_st->codec->height,
                        s->video_st->codec->pix_fmt,
                        s->video_st->codec->width,
                        s->video_st->codec->height,
                        TARGET_IMAGE_FORMAT,
                        SWS_BILINEAR,
                        NULL,
                        NULL,
                        NULL);
                __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "THIS WONT COME");
                break;
            default:
                break;
        }
        return SUCCESS;
    }

最佳答案

添加

if(s->video_st->codec->pix_fmt==-1)
                s->video_st->codec->pix_fmt=0;

在 sws_getContext 调用 ffmpeg_3.4 之前

关于android - FFMPEG 视频缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51322811/

相关文章:

video - MKV能不能装VP8 Opus组合,怎么装

android - Android App 中最用户友好的布局选项

python - FFmpeg 和 Jupyter 笔记本

ffmpeg - 使用 ffmpeg 从网络摄像头生成 RTSP

java - For循环原因: VM aborting - Fatal signal 6 (SIGABRT) code=-6

c++ - 使用 Visual Studio 2008 开发 Android 应用程序

android - 在 ViewPager 中输入动画

c# - 从 DataTable 填充 ListView

android - 不确定的 ProgressBar 使我的后台线程非常慢

android - 在 Android 上使用 ffmpeg 2.1.1 时出现 undefined reference