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

查看:20
本文介绍了在使用 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.

但是有个问题,相机支持的预览尺寸是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天全站免登陆