ffmpeg`avformat_write_header`失败,-22无效arg

时间:2017-02-03 10:25:23

标签: android ffmpeg

我在使用FFMPEG时遇到错误。这是我的代码:

static int open_output_file(const char *filename) {
    AVStream *out_stream;
    AVStream *in_stream;
    AVCodecContext *dec_ctx, *enc_ctx;
    AVCodec *encoder;
    int ret;
    unsigned int i;
    ofmt_ctx = NULL;
    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
    if (!ofmt_ctx) {
        LOGD(NULL, AV_LOG_ERROR, "Could notcreate output context\n");
        return AVERROR_UNKNOWN;
    }
    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        out_stream = avformat_new_stream(ofmt_ctx, NULL);
        if (!out_stream) {
            LOGD(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
            return AVERROR_UNKNOWN;
        }
        in_stream = ifmt_ctx->streams[i];
        dec_ctx = in_stream->codec;
        enc_ctx = out_stream->codec;
        if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
            || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
            /* in this example, we choose transcoding to same codec */

            encoder = avcodec_find_encoder(dec_ctx->codec_id);
            enc_ctx = avcodec_alloc_context3(encoder);
            if (!encoder) {
                LOGE("encoder not avalibe ");
                return -1;
            }
            /* In this example, we transcode to same properties(picture size,
            * sample rate etc.). These properties can be changed for output
            * streams easily using filters */
            if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
                enc_ctx->height = dec_ctx->height;
                enc_ctx->width = dec_ctx->width;
                enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
                /* take first format from list of supported formats */
                enc_ctx->pix_fmt = encoder->pix_fmts[0];
                /* video time_base can be set to whatever is handy andsupported by encoder */
                enc_ctx->time_base = dec_ctx->time_base;
            } else {
                enc_ctx->sample_rate = dec_ctx->sample_rate;
                enc_ctx->channel_layout = dec_ctx->channel_layout;
                enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
                /* take first format from list of supported formats */
                enc_ctx->sample_fmt = encoder->sample_fmts[0];
                AVRational time_base = {1, enc_ctx->sample_rate};
                enc_ctx->time_base = time_base;
            }
            /* Third parameter can be used to pass settings to encoder*/
            ret = avcodec_open2(enc_ctx, encoder, NULL);
            if (ret < 0) {
                LOGD("Cannot openvideo encoder for stream #%u\n", i);
                return ret;
            }
        } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
            LOGE("Elementarystream #%d is of unknown type, cannot proceed\n",
                 i);
            return AVERROR_INVALIDDATA;
        } else {
            /* if this stream must be remuxed */
            ret = avcodec_copy_context(ofmt_ctx->streams[i]->codec,
                                       ifmt_ctx->streams[i]->codec);
            if (ret < 0) {
                LOGE("Copyingstream context failed\n");
                return ret;
            }
        }
        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
            enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }
    av_dump_format(ofmt_ctx, 0, filename, 1);
    if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            LOGE("Could not open output file '%s'", filename);
            return ret;
        }
    }
    ret = avformat_write_header(ofmt_ctx, NULL); //wrong here
    if (ret < 0) {
        char buf[1024] = {0};
        av_strerror(ret, buf, 1024);
        LOGE("Error occurred when opening output file : %s\n", buf);
        return ret;
    }
    return 0;
}

这是ffmpeg日志。密切关注我得到的错误:

Error occurred when opening output file : Invalid argument

以下是其余部分:

02-03 18:09:44.120 24571-24571/com.zhenbeiju.avplayer E/videoplayer: [mov,mp4,m4a,3gp,3g2,mj2 @ 0xebf33e00] After avformat_find_stream_info() pos: 680796 bytes read:308417 seeks:1 frames:48
02-03 18:09:44.120 24571-24571/com.zhenbeiju.avplayer E/videoplayer: [h264 @ 0xea72cc00] nal_unit_type: 7, nal_ref_idc: 3
02-03 18:09:44.120 24571-24571/com.zhenbeiju.avplayer E/videoplayer: [h264 @ 0xea72cc00] nal_unit_type: 8, nal_ref_idc: 3
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/sdcard/test2.mp4':
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer:   Metadata:
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     major_brand     : 
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer: mp42
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     minor_version   : 
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 0
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     compatible_brands: 
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer: isommp42
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     creation_time   : 
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 2017-01-23T08:22:57.000000Z
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     com.android.version: 
02-03 18:09:44.121 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 6.0.1
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer:   Duration: 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 00:00:07.74
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: , start: 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 0.000000
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: , bitrate: 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 14377 kb/s
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     Stream #0:0
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: (eng)
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: , 1, 1/90000
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: : Video: h264 (avc1 / 0x31637661), yuv420p, 1280x720, 13892 kb/s
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: , SAR 1:1 DAR 16:9
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: , 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 30.02 fps, 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 30 tbr, 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 90k tbn, 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 60.04 tbc
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer:  (default)
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     Metadata:
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer:       rotate          : 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 90
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer:       creation_time   : 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 2017-01-23T08:22:57.000000Z
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer:       handler_name    : 
02-03 18:09:44.122 24571-24571/com.zhenbeiju.avplayer E/videoplayer: VideoHandle
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     Side data:
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer:       
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer: displaymatrix: rotation of -90.00 degrees
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     Stream #0:1
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer: (eng)
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer: , 47, 1/48000
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer: : Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer:  (default)
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     Metadata:
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer:       creation_time   : 
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer: 2017-01-23T08:22:57.000000Z
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer:       handler_name    : 
02-03 18:09:44.123 24571-24571/com.zhenbeiju.avplayer E/videoplayer: SoundHandle
02-03 18:09:44.124 24571-24571/com.zhenbeiju.avplayer E/videoplayer: [libx264 @ 0xdbf3dc00] using mv_range_thread = 56
02-03 18:09:44.126 24571-24571/com.zhenbeiju.avplayer E/videoplayer: [libx264 @ 0xdbf3dc00] using cpu capabilities: none!
02-03 18:09:44.197 24571-24571/com.zhenbeiju.avplayer E/videoplayer: [libx264 @ 0xdbf3dc00] profile High, level 4.0
02-03 18:09:44.199 24571-24571/com.zhenbeiju.avplayer E/videoplayer: Output #0, mp4, to '/sdcard/test3.mp4':
02-03 18:09:44.199 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     Stream #0:0
02-03 18:09:44.199 24571-24571/com.zhenbeiju.avplayer E/videoplayer: , 0, 0/0
02-03 18:09:44.199 24571-24571/com.zhenbeiju.avplayer E/videoplayer: : Unknown: none
02-03 18:09:44.200 24571-24571/com.zhenbeiju.avplayer E/videoplayer:     Stream #0:1
02-03 18:09:44.200 24571-24571/com.zhenbeiju.avplayer E/videoplayer: , 0, 0/0
02-03 18:09:44.200 24571-24571/com.zhenbeiju.avplayer E/videoplayer: : Unknown: none
02-03 18:09:44.200 24571-24571/com.zhenbeiju.avplayer E/videoplayer: [file @ 0xdbdc71e0] Setting default whitelist 'file,crypto'
02-03 18:09:44.201 24571-24571/com.zhenbeiju.avplayer E/videoplayer: [mp4 @ 0xebf3a800] Could not find tag for codec none in stream #0, codec not currently supported in container
02-03 18:09:44.201 24571-24571/com.zhenbeiju.avplayer E/videoplayer: Error occurred when opening output file : Invalid argument

0 个答案:

没有答案