在Andorid中使用FFmpeg实现YUV编码为MP4

来源:互联网 发布:天池 大数据 编辑:程序博客网 时间:2024/05/17 03:56

在Andorid中使用FFmpeg实现YUV编码为MP4

最进根据公司项目需要,学习FFmpeg音视频编解码做技术储备,项目是运行在android平台上的,现在需求还不确定,可能需要YUV编码为MP4。

activity_main.xml主要代码

   <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <TextView            android:id="@+id/tv_route_5"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:gravity="center_vertical"            app:layout_constraintBottom_toBottomOf="parent"            app:layout_constraintLeft_toLeftOf="parent"            app:layout_constraintRight_toRightOf="parent"            app:layout_constraintTop_toTopOf="parent"/>        <Button            android:id="@+id/bt_yuv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="选择YUV"            />        <Button            android:id="@+id/bt5"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="压缩"            />    </LinearLayout>

bt_yuv打开文件管理器选择文件,tv_route_5显示文件路径,bt5开始编码。

MainActivity.java 核心代码

    static {        System.loadLibrary("native-lib");    }   public native int yuvToMp4(String input_jstr, String output_jstr, int w_jstr, int h_jstr, int num_jstr);

在Android中使用FFmpeg可以参考 在Android Studio中使用cmake编译FFmpeg

    public void openSystemFile(int type) {        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);        //系统调用Action属性        intent.setType("*/*");        //设置文件类型        intent.addCategory(Intent.CATEGORY_OPENABLE);        // 添加Category属性        try {            startActivityForResult(intent, type);        } catch (Exception e) {            Toast.makeText(this, "没有正确打开文件管理器", Toast.LENGTH_SHORT).show();        }    } @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (resultCode == Activity.RESULT_OK) {//是否选择,没选择就不会继续                      Uri uri = data.getData();//得到uri,后面就是将uri转化成file的过程。                String[] proj = {MediaStore.Images.Media.DATA};                Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);                int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);                actualimagecursor.moveToFirst();                String img_path = actualimagecursor.getString(actual_image_column_index);                if ( img_path.endsWith(".yuv")) {                    if (requestCode == 5) {                        tvRoute5.setText(img_path);                    }                } else {                    Toast.makeText(this, "请选择正确的文件", Toast.LENGTH_SHORT).show();                }            }      }    /**     * 压缩按钮,开始转Mp4     */    private void compress(final TextView tvRoute, final int w_jstr, final int h_jstr, final int num_jstr) {        dialog.showDialog();        String[] videoInfo = tvRoute.getText().toString().split("/");        String fileName = videoInfo[videoInfo.length - 1];        final String[] fileNames = fileName.split("\\.");        new Thread(new Runnable() {            public void run() {                int result = yuvToMp4(tvRoute.getText().toString(), "/storage/emulated/0/Download/avtest/" + fileNames[0] + ".mp4", w_jstr, h_jstr, num_jstr);                //转码成功                if (result >= 0) {                    handler.sendEmptyMessage(2);                } else {                    handler.sendEmptyMessage(1);                }            }        }).start();    }   Handler handler = new Handler(new Handler.Callback() {        @Override        public boolean handleMessage(Message message) {            if (message.what == 1) {                Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();                dialog.dismiss();            } else if (message.what == 2) {                Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();                dialog.dismiss();            }            return false;        }    });

调用系统的文件管理器,选择YUV文件,在子线程中调用yuvToMp4将其转化为MP4,然后在Handler中更新UI。

native-lib.cpp核心代码

int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index);/*** * yuv转换成mp4 */extern "C"JNIEXPORT jint JNICALLJava_com_yodosmart_ffmpegdemo_MainActivity_yuvToMp4(JNIEnv *env, jobject instance,                                                    jstring input_jstr_, jstring output_jstr_,                                                    jint w_jstr_, jint h_jstr_, jint num_jstr_) {    const char *input_jstr = env->GetStringUTFChars(input_jstr_, 0);    const char *output_jstr = env->GetStringUTFChars(output_jstr_, 0);    AVFormatContext *pFormatCtx;    AVOutputFormat *fmt;    AVStream *video_st;    AVCodecContext *pCodecCtx;    AVCodec *pCodec;    AVPacket pkt;    uint8_t *picture_buf;    AVFrame *pFrame;    int picture_size;    int y_size;    int framecnt = 0;    //FILE *in_file = fopen("src01_480x272.yuv", "rb"); //Input raw YUV data    FILE *in_file = fopen(input_jstr, "rb");   //Input raw YUV data    int in_w = w_jstr_, in_h = h_jstr_;                              //Input data's width and height    int framenum = num_jstr_;                                   //Frames to encode    //const char* out_file = "src01.h264";              //Output Filepath    //const char* out_file = "src01.ts";    //const char* out_file = "src01.hevc";    const char *out_file = output_jstr;    av_register_all();    //Method1.    pFormatCtx = avformat_alloc_context();    //Guess Format    fmt = av_guess_format(NULL, out_file, NULL);    pFormatCtx->oformat = fmt;    //Method 2.    //avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);    //fmt = pFormatCtx->oformat;    //Open output URL    if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {        printf("Failed to open output file! \n");        return -1;    }    video_st = avformat_new_stream(pFormatCtx, 0);    //video_st->time_base.num = 1;    //video_st->time_base.den = 25;    if (video_st == NULL) {        return -1;    }    //Param that must set    pCodecCtx = video_st->codec;    //pCodecCtx->codec_id =AV_CODEC_ID_HEVC;    pCodecCtx->codec_id = fmt->video_codec;    pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;    pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;    pCodecCtx->width = in_w;    pCodecCtx->height = in_h;    pCodecCtx->bit_rate = 400000;    pCodecCtx->gop_size = 250;    pCodecCtx->time_base.num = 1;    pCodecCtx->time_base.den = 25;    //H264    //pCodecCtx->me_range = 16;    //pCodecCtx->max_qdiff = 4;    //pCodecCtx->qcompress = 0.6;    pCodecCtx->qmin = 10;    pCodecCtx->qmax = 51;    //Optional Param    pCodecCtx->max_b_frames = 3;    // Set Option    AVDictionary *param = 0;    //H.264    if (pCodecCtx->codec_id == AV_CODEC_ID_H264) {        av_dict_set(&param, "preset", "slow", 0);        av_dict_set(&param, "tune", "zerolatency", 0);        //av_dict_set(¶m, "profile", "main", 0);    }    //H.265    if (pCodecCtx->codec_id == AV_CODEC_ID_H265) {        av_dict_set(&param, "preset", "ultrafast", 0);        av_dict_set(&param, "tune", "zero-latency", 0);    }    //Show some Information    av_dump_format(pFormatCtx, 0, out_file, 1);    pCodec = avcodec_find_encoder(pCodecCtx->codec_id);    if (!pCodec) {        printf("Can not find encoder! \n");        return -1;    }    if (avcodec_open2(pCodecCtx, pCodec, &param) < 0) {        printf("Failed to open encoder! \n");        return -1;    }    pFrame = av_frame_alloc();    picture_size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);    picture_buf = (uint8_t *) av_malloc(picture_size);    avpicture_fill((AVPicture *) pFrame, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width,                   pCodecCtx->height);    //Write File Header    avformat_write_header(pFormatCtx, NULL);    av_new_packet(&pkt, picture_size);    y_size = pCodecCtx->width * pCodecCtx->height;    for (int i = 0; i < framenum; i++) {        //Read raw YUV data        if (fread(picture_buf, 1, y_size * 3 / 2, in_file) <= 0) {            printf("Failed to read raw data! \n");            return -1;        } else if (feof(in_file)) {            break;        }        pFrame->data[0] = picture_buf;              // Y        pFrame->data[1] = picture_buf + y_size;      // U        pFrame->data[2] = picture_buf + y_size * 5 / 4;  // V        //PTS        //pFrame->pts=i;        pFrame->pts = i * (video_st->time_base.den) / ((video_st->time_base.num) * 25);        int got_picture = 0;        //Encode        int ret = avcodec_encode_video2(pCodecCtx, &pkt, pFrame, &got_picture);        if (ret < 0) {            printf("Failed to encode! \n");            return -1;        }        if (got_picture == 1) {            printf("Succeed to encode frame: %5d\tsize:%5d\n", framecnt, pkt.size);            framecnt++;            pkt.stream_index = video_st->index;            ret = av_write_frame(pFormatCtx, &pkt);            av_free_packet(&pkt);        }    }    //Flush Encoder    int ret = flush_encoder(pFormatCtx, 0);    if (ret < 0) {        printf("Flushing encoder failed\n");        return -1;    }    //Write file trailer    av_write_trailer(pFormatCtx);    //Clean    if (video_st) {        avcodec_close(video_st->codec);        av_free(pFrame);        av_free(picture_buf);    }    avio_close(pFormatCtx->pb);    avformat_free_context(pFormatCtx);    fclose(in_file);    return 0;}int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index) {    int ret;    int got_frame;    AVPacket enc_pkt;    if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &          CODEC_CAP_DELAY))        return 0;    while (1) {        enc_pkt.data = NULL;        enc_pkt.size = 0;        av_init_packet(&enc_pkt);        ret = avcodec_encode_video2(fmt_ctx->streams[stream_index]->codec, &enc_pkt,                                    NULL, &got_frame);        av_frame_free(NULL);        if (ret < 0)            break;        if (!got_frame) {            ret = 0;            break;        }        printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", enc_pkt.size);        /* mux encoded frame */        ret = av_write_frame(fmt_ctx, &enc_pkt);        if (ret < 0)            break;    }    return ret;}

代码下载

参考资料
http://blog.csdn.net/leixiaohua1020/article/details/25430425