在使用 MediaCodec 为 Grafika 的“连续捕获"进行编码之前裁剪视频活动 [英] Crop video before encoding with MediaCodec for Grafika's "Continuous Capture" Activity

查看:38
本文介绍了在使用 MediaCodec 为 Grafika 的“连续捕获"进行编码之前裁剪视频活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Grafika 的Continuous Capture" Activity,它是关于使用 MediaCodec 录制视频.

I am learning about Grafika's "Continuous Capture" Activity, It is about recording a video with MediaCodec.

活动源代码位于https://github.com/google/grafika/blob/master/src/com/android/grafika/ContinuousCaptureActivity.java

程序使用一个 SurfaceTexture obj 从相机接收数据,并使用这个 SurfaceTexture obj 创建 2 个 EGLSurface obj,一个 EGLSurface obj 将数据提供给 MediaCodec,另一个将数据提供给 SurfaceView 用于相机预览.MediaCodec 将数据编码为 h264 数据,MediaMuxer obj 将 h264 数据写入 mp4 文件.

The program uses a SurfaceTexture obj to receive data from camera and creates 2 EGLSurface obj with this SurfaceTexture obj, one EGLSurface obj feeds the data to MediaCodec and the other feeds data to SurfaceView for camera preview. MediaCodec encodes the data to h264 data and the MediaMuxer obj writes h264 data to a mp4 file.

但是有一个问题,相机支持的预览尺寸是landspace(宽>高)如1920*1080、1440*1080、720*480等.通常我们在录视频的时候手机是纵向的,所以我们应该使用API​​:Camera.setDisplayOrientation(90) 将图片旋转为纵向,然后录制纵向视频.

But there is a problem, the preview size supported by the camera is landspace (width > height) such as 1920*1080, 1440*1080,720*480 and so on. Usually, we take the phone in portrait orientation when we record a video, so we should use API: Camera.setDisplayOrientation(90) to rotate the picture to a portrait one, then a portrait video will be recorded.

但我想用手中的手机人像录制横向视频,我必须从相机中裁剪每一帧.我的方法是把每一帧画面的底部和顶部都剪掉,保留画面的中间,那么左边的画面就是横向的了.

But I want to record a landscape video with the phone portrait in my hand, I have to crop every frame from the camera. My method is that cutting off the bottom and top of every frame picture and retain the middle of the picture, then the left picture will be a landscapce one.

但是我对opengl不熟悉,不知道如何裁剪SurfaceTexture数据.有哪位擅长opengl的可以帮帮我吗?

But I am not familiar with opengl, I do not know how to crop the SurfaceTexture data. Could anyone who is good at opengl give me some help?

推荐答案

看看来自相机的纹理"活动.请注意,它允许您以各种方式操纵图像,尤其是缩放".缩放"是通过修改纹理坐标来完成的.

Take a look at the "texture from camera" activity. Note it allows you to manipulate the image in various ways, notably "zoom". The "zoom" is done by modifying the texture coordinates.

ScaledDrawable2D 类这;setScale() 调用更改缩放",而不是缩放矩形本身.纹理坐标范围从 0.0 到 1.0(含),getTexCoordArray() 方法将它们修改为跨越纹理的子集.

The ScaledDrawable2D class does this; the setScale() call changes the "zoom", rather than scaling the rect itself. Texture coordinates range from 0.0 to 1.0 inclusive, and the getTexCoordArray() method modifies them to span a subset of the texture.

要剪辑帧,您需要按比例修改纹理坐标.例如,如果输入视频是纵向 720x1280,而您想要 720x720,则可以从以下位置更改坐标:

To clip the frames, you'd need to modify the texture coordinates proportionally. For example, if the input video is portrait 720x1280, and you want 720x720, you would change the coordinates from this:

[0.0, 0.0]  [1.0, 0.0]
[0.0, 1.0]  [1.0, 1.0]

到这里:

[0.0, 280/1280.0]  [1.0, 280/1280.0]
[0.0, 1000/1280.0] [1.0, 1000/1280.0]

然后将其渲染在正方形而不是矩形上.

and then render that on a square rather than a rectangle.

这篇关于在使用 MediaCodec 为 Grafika 的“连续捕获"进行编码之前裁剪视频活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆