android 集成ffmpeg 编码摄像头数据 mpegts格式 mpegvideo编码 出错
第一步 初始化
Java_com_deerlive_jni_ffmpeg_FFmpegHandle_initVideo(JNIEnv *env, jobject instance,
jstring url_) {
const char *out_path = env->GetStringUTFChars(url_, 0);
logd(out_path);
//计算yuv数据的长度
yuv_width = width;
yuv_height = height;
y_length = width * height;
uv_length = width * height / 4;
av_register_all();
//推流就需要初始化网络协议
avformat_network_init();
//初始化AVFormatContext
avformat_alloc_output_context2(&ofmt_ctx, NULL, "mpegts", out_path);
if(!ofmt_ctx) {
loge("Could not create output context\n");
return -1;
}
//寻找编码器,这里用的就是x264的那个编码器了
pCodec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);
if(!pCodec) {
loge("Can not find encoder!\n");
return -1;
}
//初始化编码器的context
pCodecCtx = avcodec_alloc_context3(pCodec);
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; //指定编码格式
pCodecCtx->width = width;
pCodecCtx->height = height;
pCodecCtx->time_base.num = 1;
pCode