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

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

问题描述

我正在了解Grafika的连续捕获"活动,它是关于使用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.

但是有一个问题,相机支持的预览尺寸是陆地空间(宽度>高度),例如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天全站免登陆