在Andorid中使用FFmpeg实现MP4/AVI/H.264解析为BMP:MP4/AVI/H.264解析为RGB
最进根据公司项目需要,学习FFmpeg音视频编解码做技术储备,项目是运行在android平台上的,现在需求还不确定,需要MP4/AVI/H.264视频转换为一张张BMP,先把MP4/AVI/H.264转换为RGB,再把RGB转化为BMP,本篇主要实现MP4/AVI/H.264转换为RGB/YUV,以MP4为例,AVI/H.264同MP4。
RGB解析为BMP参考 在Andorid中使用FFmpeg实现MP4/AVI/H.264解析为BMP:RGB解析为BMP
activity_main.xml关键代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_route_1"
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_mp4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择mp4"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解析图片"
/>
</LinearLayout>
bt_mp4选择MP4视频文件,tv_route_1显示视频文件路径,bt1开始解析。
###MainActivity.java关键代码
加载库文件
static {
System.loadLibrary("native-lib");
}
public native int avToRGB(String input_jstr, String output_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(".mp4")) {
if (requestCode == 1) {
tvRoute1.setText(img_path);
}
} else {
Toast.makeText(this, "请选择正确的文件", Toast.LENGTH_SHORT).show();
}
}
}
开始转码
/**
* 确认按钮,开始转码
*/
private void conversion(final TextView tvRoute) {
dialog.showDialog();
String[] videoInfo = tvRoute.getText().toString().split("/");
String fileName = videoInfo[videoInfo.length - 1];
String filePath = tvRoute.getText().toString().replace(fileName, "");
final String[] fileNames = fileName.split("\\.");
new Thread(new Runnable() {
public void run() {
picNum = avToRGB(tvRoute.getText().toString(), "/storage/emulated/0/Download/avtest/" + fileNames[0] + ".rgb");
//转码成功
if (picNum >= 0) {
handler.sendEmptyMessage(0);
} else {
handler.sendEmptyMessage(1);
}
}
}).start();
}
Handler 更新UI
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
if (message.what == 0) {
dialog.dismiss();
} else if (message.what == 1) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
return false;
}
});
###native-lib.cpp 核心方法
/***
* avi/mp4/h264转换成YUV/RGB
* avi/mp4/h264->RGB->BMP
*/
extern "C"
JNIEXPORT jint JNICALL
Java_com_yodosmart_ffmpegdemo_MainActivity_avToRGB(JNIEnv *env, jobject instance,
jstring input_jstr, jstring output_jstr) {
//h264ToYue
AVFormatContext *pFormatCtx;
int i, videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameRGB;
uint8_t *out_buffer;
AVPacket *packet;
int y_size;
int ret, got_picture;
struct SwsContext *img_convert_ctx;
struct SwsContext *img_convert_ctx_rgb;
FILE *fp_yuv;
int frame_cnt;
clock_t time_start, time_finish;
double time_duration = 0.0;
char input_str[500] = {0};
char output_str[500] = {0};
char info[1000] = {0};
sprintf(input_str, "%s", env->GetStringUTFChars(input_jstr, NULL));
sprintf(output_str, "%s", env->GetStringUTFChars(output_jstr, NULL));
FILE *output = fopen(output_str, "wb+");
//FFmpeg av_log() callback
av_log_set_callback(custom_log);
av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
if (avformat_open_input(&pFormatCtx, input_str, NULL, NULL) != 0) {
LOGE("Couldn't open input stream.\n");
return -1;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
LOGE("Couldn't find stream information.\n");
return -1;
}
videoindex = -1;
for (i = 0; i < pFormatCtx->nb_streams; i++)
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoindex = i;
break;
}
if (videoindex == -1) {
LOGE("Couldn't find a video stream.\n");
return -1;
}
pCodecCtx = pFormatCtx->streams[videoindex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
LOGE("Couldn't find Codec.\n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
LOGE("Couldn't open codec.\n");
return -1;
}
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc();
// out_buffer = (unsigned char *) av_malloc(
// av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));
// av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, out_buffer,
// AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
out_buffer = (unsigned char *) av_malloc(
av_image_get_buffer_size(AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height, 1));
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, out_buffer,
AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height, 1);
packet = (AVPacket *) av_malloc(sizeof(AVPacket));
// img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
// pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P,
// SWS_BICUBIC, NULL, NULL, NULL);
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24,
SWS_BICUBIC, NULL, NULL, NULL);
sprintf(info, "[Input ]%s\n", input_str);
sprintf(info, "%s[Output ]%s\n", info, output_str);
sprintf(info, "%s[Format ]%s\n", info, pFormatCtx->iformat->name);
sprintf(info, "%s[Codec ]%s\n", info, pCodecCtx->codec->name);
sprintf(info, "%s[Resolution]%dx%d\n", info, pCodecCtx->width, pCodecCtx->height);
fp_yuv = fopen(output_str, "wb+");
if (fp_yuv == NULL) {
printf("Cannot open output file.\n");
return -1;
}
frame_cnt = 0;
time_start = clock();
while (av_read_frame(pFormatCtx, packet) >= 0) {
if (packet->stream_index == videoindex) {
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if (ret < 0) {
LOGE("Decode Error.\n");
return -1;
}
if (got_picture) {
sws_scale(img_convert_ctx, (const uint8_t *const *) pFrame->data, pFrame->linesize,
0, pCodecCtx->height,
pFrameRGB->data, pFrameRGB->linesize);
y_size = pCodecCtx->width * pCodecCtx->height;
//RGB
//转换
fwrite(pFrameRGB->data[0], (pCodecCtx->width) * (pCodecCtx->height) * 3, 1, output);
//fwrite(pFrameRGB->data[0], 1, y_size, fp_yuv); //Y
//fwrite(pFrameRGB->data[1], 1, y_size / 4, fp_yuv); //U
//fwrite(pFrameRGB->data[2], 1, y_size / 4, fp_yuv); //V
//Output info
char pictype_str[10] = {0};
switch (pFrame->pict_type) {
case AV_PICTURE_TYPE_I:
sprintf(pictype_str, "I");
break;
case AV_PICTURE_TYPE_P:
sprintf(pictype_str, "P");
break;
case AV_PICTURE_TYPE_B:
sprintf(pictype_str, "B");
break;
default:
sprintf(pictype_str, "Other");
break;
}
LOGI("Frame Index: %5d. Type:%s", frame_cnt, pictype_str);
frame_cnt++;
}
}
av_free_packet(packet);
}
//flush decoder
//FIX: Flush Frames remained in Codec
while (1) {
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if (ret < 0)
break;
if (!got_picture)
break;
// sws_scale(img_convert_ctx, (const uint8_t *const *) pFrame->data, pFrame->linesize, 0,
// pCodecCtx->height,
// pFrameRGB->data, pFrameRGB->linesize);
// int y_size = pCodecCtx->width * pCodecCtx->height;
sws_scale(img_convert_ctx, (const uint8_t *const *) pFrame->data, pFrame->linesize, 0,
pCodecCtx->height,
pFrameRGB->data, pFrameRGB->linesize);
int y_size = pCodecCtx->width * pCodecCtx->height;
//RGB
//转换
fwrite(pFrameRGB->data[0], (pCodecCtx->width) * (pCodecCtx->height) * 3, 1, output);
// fwrite(pFrameRGB->data[0], 1, y_size, fp_yuv); //Y
// fwrite(pFrameRGB->data[1], 1, y_size / 4, fp_yuv); //U
// fwrite(pFrameRGB->data[2], 1, y_size / 4, fp_yuv); //V
//Output info
char pictype_str[10] = {0};
switch (pFrame->pict_type) {
case AV_PICTURE_TYPE_I:
sprintf(pictype_str, "I");
break;
case AV_PICTURE_TYPE_P:
sprintf(pictype_str, "P");
break;
case AV_PICTURE_TYPE_B:
sprintf(pictype_str, "B");
break;
default:
sprintf(pictype_str, "Other");
break;
}
LOGI("Frame Index: %5d. Type:%s", frame_cnt, pictype_str);
frame_cnt++;
}
time_finish = clock();
time_duration = (double) (time_finish - time_start);
sprintf(info, "%s[Time ]%fms\n", info, time_duration);
sprintf(info, "%s[Count ]%d\n", info, frame_cnt);
sws_freeContext(img_convert_ctx);
fclose(fp_yuv);
av_frame_free(&pFrameRGB);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return frame_cnt;
}
上述代码主要实现:
avi/mp4/h264转换成RGB,注释中的代码是转换为YUV
注意: 如果转换的结果是RGB24,但是这样出来的RGB数据,写成的BMP文件,颜色错误。
修改成BRG24问题得到解决
参考资料:
http://blog.csdn.net/sweibd/article/details/52516123
http://blog.csdn.net/leixiaohua1020/article/details/47010637
http://blog.csdn.net/leixiaohua1020/article/details/42134965
欢迎关注我的公众号,持续分析优质技术文章