【问题标题】:Rotate video with Mp4parser使用 Mp4parser 旋转视频
【发布时间】:2013-10-04 20:18:21
【问题描述】:

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

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

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

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

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 解析器旋转视频吗?英语不是我的母语,因此如有语法错误,我深表歉意。

【问题讨论】:

    标签: android video video-recording mp4parser


    【解决方案1】:
    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;
    }
    

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

    【讨论】:

    • 不知何故 isoFile.getMovieBox() 总是为我返回 null,它可能与文件格式有关吗? Iso-type wise..?
    • 有效!谢谢凯莉!这是唯一且明确的答案。
    • 嗨,我旋转成功了,但是有一些问题,如果我试图将第一部电影旋转到 ROTATE_90,第二部电影旋转到 ROTATE_270 那么...第二部电影不是 ROTATE_270,而是它的旋转到 ROTATE_90 .你有什么解决办法吗...?
    • 面临与此链接中提到的相同问题:stackoverflow.com/questions/23764889/…
    • 我有一个不是使用 mp4parser 创建的视频,我想用它来旋转视频。我迷失在翻译中!关于如何用普通视频实现它的任何想法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2011-04-25
    • 2015-01-15
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多