視頻剪輯mp4parser

可以直接使用

public class VideoClipUtil {
    private static final String TAG = "VideoClipUtil";
    private String filePath;//視頻路徑
    private String workingPath;//輸出路徑
    private String outName;//輸出文件名
    private double startTime;//剪切起始時間
    private double endTime;//剪切結束時間

    public VideoClipUtil() {
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getFilePath() {
        return filePath;
    }

    public String getWorkingPath() {
        return workingPath;
    }

    public String getOutName() {
        return outName;
    }

    public void setWorkingPath(String workingPath) {
        this.workingPath = workingPath;
    }

    public void setOutName(String outName) {
        this.outName = outName;
    }

    public void setEndTime(double endTime) {
        this.endTime = endTime / 1000;
    }

    public void setStartTime(double startTime) {
        this.startTime = startTime / 1000;
    }

    public void clip() {
        try {
            //將要剪輯的視頻文件
            Movie movie = MovieCreator.build(filePath);

            List<Track> tracks = movie.getTracks();
            movie.setTracks(new LinkedList<Track>());
            //時間是否修正
            boolean timeCorrected = false;

            //計算並換算剪切時間
            for (Track track : tracks) {
                if (track.getSyncSamples() != null
                        && track.getSyncSamples().length > 0) {
                    if (timeCorrected) {
                        throw new RuntimeException(
                                "The startTime has already been corrected by another track with SyncSample. Not Supported.");
                    }
                    //true,false表示短截取;false,true表示長截取
                    startTime = VideoHelper.correctTimeToSyncSample(track, startTime, false);//修正後的開始時間
                    endTime = VideoHelper.correctTimeToSyncSample(track, endTime, true);     //修正後的結束時間
                    timeCorrected = true;
                }
            }
            //根據換算到的開始時間和結束時間來截取視頻
            for (Track track : tracks) {
                long currentSample = 0; //視頻截取到的當前的位置的時間
                double currentTime = 0; //視頻的時間長度
                double lastTime = -1;    //上次截取到的最後的時間
                long startSample1 = -1;  //截取開始的時間
                long endSample1 = -1;    //截取結束的時間

                //設置開始剪輯的時間和結束剪輯的時間  避免超出視頻總長
                for (int i = 0; i < track.getSampleDurations().length; i++) {
                    long delta = track.getSampleDurations()[i];
                    if (currentTime > lastTime && currentTime <= startTime) {
                        startSample1 = currentSample;//編輯開始的時間
                    }
                    if (currentTime > lastTime && currentTime <= endTime) {
                        endSample1 = currentSample;  //編輯結束的時間
                    }
                    lastTime = currentTime;          //上次截取到的時間(避免在視頻最後位置了還在增加編輯結束的時間)
                    currentTime += (double) delta
                            / (double) track.getTrackMetaData().getTimescale();//視頻的時間長度
                    currentSample++;                 //當前位置+1
                }
                movie.addTrack(new CroppedTrack(track, startSample1, endSample1));// 創建一個新的視頻文件
            }

            //合成視頻mp4
            Container out = new DefaultMp4Builder().build(movie);
            File storagePath = new File(workingPath);
            storagePath.mkdirs();
            FileOutputStream fos = new FileOutputStream(new File(storagePath, outName));
            FileChannel fco = fos.getChannel();
            out.writeContainer(fco);
            //關閉流
            fco.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章