使用 Mp4parser 旋转视频 [英] Rotate video with Mp4parser

查看:50
本文介绍了使用 Mp4parser 旋转视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要旋转视频来调整我的一些需求.我将解释以下列表中的详细信息.

I need to rotate a video to adjust some of my needs. I'll explain the details on the following list.

我正在创建一个类似 Vine 的应用程序.我必须录制视频片段,然后将所有部分合并到一个文件中.我在使用最新版本 1.0-RC-26 的 mp4 解析器库的 Android 应用程序上使用他们网站上提供的示例执行此操作没有问题:这里

I'm creating a Vine like app. I have to record video segments and then merge all the parts into just one file. I'm doing this without issue on an Android app using mp4 parser library with last version 1.0-RC-26 using the example provided on their website: here

如果所有视频都具有相同的方向,则附加视频示例工作正常,但我发现从前置摄像头录制视频存在一些问题,因此快速解决方案是将视频方向录制设置为 270.此解决方案的缺点是具有此方向的片段在合并的视频中以错误的方向出现.

The append video example works fine if all the videos have the same orientation but I discovered some issues recording video from the front camera so the quick solution was to set the video orientation recording on 270. The bad part on this solution is that the segment with this orientation appear with the wrong orientation on the merged video.

我可能的解决方案是旋转视频以在不同情况下应用我需要的内容,但我的代码没有工作示例.在互联网上搜索我找到了这样的解决方案这里.这段代码的问题是与上一个版本不兼容(它给出了一个编译错误).我也试图理解图书馆的逻辑,但我没有结果.例如,我尝试在 Movie 对象上使用 setMatrix 指令,但它根本不起作用.

My possible solution to this is to rotate the video to apply what I need in different situations but I'm not having a working example with my code. Searching the internet I found solutions like this one here. The problem with this code is that is not compatible with the last version (It gives an compilation error) . I tried too to understand the logic of the library but I'm not having results. For example I experimented using the setMatrix instruction on the Movie object but It simply don't work.

public static void mergeVideo(int SegmentNumber) throws Exception {

    Log.d("PM", "Merge process started");
     Movie[] inMovies = new Movie[SegmentNumber]   ;
     //long[] Matrix = new long[SegmentNumber];

     for (int i = 1 ; i <= SegmentNumber; i++){
         File file =  new File(getCompleteFilePath(i));
         if (file.exists()){
             FileInputStream fis = new FileInputStream(getCompleteFilePath(i));
             //Set rotation I tried to experiment with this instruction but is not working
             inMovies [i-1].setMatrix(Matrix.ROTATE_90);
             inMovies [i-1] = MovieCreator.build(fis.getChannel());

             Log.d("PM", "Video " + i  + " merged" );
         }

         //fis.close();
     }


        List<Track> videoTracks = new LinkedList<Track>();
        List<Track> audioTracks = new LinkedList<Track>();

        for (Movie m : inMovies) {
            for (Track t : m.getTracks()) {
                if (t.getHandler().equals("soun")) {
                    audioTracks.add(t);
                }
                if (t.getHandler().equals("vide")) {
                    videoTracks.add(t);
                }
            }
        }

        Movie result = new Movie();

        if (audioTracks.size() > 0) {
            result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
        }
        if (videoTracks.size() > 0) {
            result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
        }

        Container out = new DefaultMp4Builder().build(result);

        //out.getMovieBox().getMovieHeaderBox().setMatrix(Matrix.ROTATE_180); //set orientation, default merged video have wrong orientation
        // Create a media file name
        //
        String filename =  getCompleteMergedVideoFilePath()  ;

        FileChannel fc = new RandomAccessFile(String.format(filename), "rw").getChannel();
        out.writeContainer(fc);
        fc.close();


        //don't leave until the file is on his place
        File file = new File (filename);
        do {
            if (! file.exists()){
                Log.d("PM", "Result file not ready");
            }
       } while (! file.exists() );
       //
        Log.d("PM", "Merge process finished");
}

有人使用最新版本的 Mp4 解析器旋转视频吗?英语不是我的母语,因此对于任何语法错误,我深表歉意.

Have someone rotated video with the very last version of Mp4 parser? English is not my native language so I apologize any grammar error.

推荐答案

for (int i = 1; i <= SegmentNumber; i++) {

    IsoFile isoFile = new IsoFile(getCompleteFilePath(i));
    Movie m = new Movie();

    List<TrackBox> trackBoxes = isoFile.getMovieBox().getBoxes(
    TrackBox.class);

    for (TrackBox trackBox : trackBoxes) {
        trackBox.getTrackHeaderBox().setMatrix(Matrix.ROTATE_90);
        m.addTrack(new Mp4TrackImpl(trackBox));
    }

    inMovies[i - 1] = m;
}

这是我为旋转视频所做的.

This is what I did to rotate a video.

这篇关于使用 Mp4parser 旋转视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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