MP4Parser改变了视频方向 [英] MP4Parser change video orientation
问题描述
我尝试使用MP4Parser(或任何其他任何方法,如果你知道其中一个)将当前正在使用TrackHeaderBox旋转的横向视频转换为肖像,但无法完全改变方向,有人在此之前使用过可以发现我可能犯的错误吗?任何帮助都会有很长的路要谢谢
Im attempting to rotate a video that is in landscape into portrait using MP4Parser (or any other method if you know of one) currently playing with the TrackHeaderBox but unable to get the orientation to change at all, has anyone used this before that can spot the mistake I may of made? any help will go a long way thanks
IsoFile out = new DefaultMp4Builder().build(result);
// test
double[] m = null;
m = new double[] { 0.0, 1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
TrackBox tb = out.getMovieBox().getBoxes(TrackBox.class).get(0);
TrackHeaderBox box = tb.getTrackHeaderBox();
box.setMatrix(m);
推荐答案
在MP4中嵌入有关轮换的信息
您是否真的在改变视频轨道的方向?如果更改音轨的方向,则不会看到任何更改。
Embedding Information about Rotation in the MP4
Are you really changing the orientation of the video track? If you change the orientation of the audio track you will not see any change.
根据我的经验,更改整个文件的方向更容易(1.0.4.2 API版本):
From my experience it's easier to change the orientation of the whole file (1.0.4.2 API Version):
Movie result = MovieCreator.build("input.mp4");
// do something with the file
Container out = new DefaultMp4Builder().build(result);
MovieHeaderBox mvhd = Path.getPath(out, "moov/mvhd");
mvhd.setMatrix(Matrix.ROTATE_180);
out.writeContainer(new FileOutputStream("result.mp4").getChannel());
或者如果你想直接改变方向而不通过电影
object:
alternatively if you want to change the orientation directly without going via a Movie
object:
IsoFile isoFile = new IsoFile("video.mp4");
MovieHeaderBox mvhd = Path.getPath(isoFile, "/moov/mvhd");
mvhd.setMatrix(Matrix.ROTATE_180);
isoFile.writeContainer(new FileOutputStream("result.mp4").getChannel());
文件 result.mp4
现已轮换您可以通过在桌面播放器(如QuickTime或VLC)中播放文件来验证180度。
The file result.mp4
is now rotated by 180 degrees as you can verify by playing back the file in a desktop player such as QuickTime or VLC.
当您在VideoView的帮助下在Android上播放视频时,您可能会注意到矩阵没有考虑在内。我不完全确定这是否是故意的,但解决方法是使用应用转换的 TextureView
。
When you playback the video on Android with the help of the VideoView you might notice that the matrix is not taken into account. I'm not entirely sure if this is done on purpose or not but the workaround is to use a TextureView
that applies the transformation.
为了做到这一点,你必须
In order to do so you have to
- 从中提取矩阵位于/ moov / mvhd的MovieHeaderBox和位于/ moov / trak [0,1,2,3] / tkhd的MediaHeaderBox(取决于哪个trak包含视频)。
- 通过矩阵乘法合并两个矩阵。
- 致电
setScaleX
,setScaleY
,setPivotX
,setPivotY
和setRotation
,其值根据之前步骤的结果矩阵。
- extract the matrices from the MovieHeaderBox at /moov/mvhd and from MediaHeaderBox at /moov/trak[0, 1, 2, 3]/tkhd (depending on which trak contains the video).
- Combine both matrices via matrix multiplication.
- Call
setScaleX
,setScaleY
,setPivotX
,setPivotY
andsetRotation
with values according to the resulting matrix from the step before.
这篇关于MP4Parser改变了视频方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!