视频剪辑mp4parser

来源:互联网 发布:java的安装教程 编辑:程序博客网 时间:2024/05/20 08:25

可以直接使用

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();        }    }}
原创粉丝点击