Android Studio使用mp4parser进行视频的分割( 二 )

< 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));// 创建一个新的视频文件}//合成视频mp4Container 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();}}}
.java(换算时间使用的工具类,可与.java合并)
import com.googlecode.mp4parser.authoring.Track;import java.util.Arrays;/*** @ClassName: VideoHelper* @Description:* @version: V2.0* @Copyright: X-Force* @author: foryou* @Date: 2017/2/20 14:25*/public class VideoHelper {/*** 换算剪切时间*/public static double correctTimeToSyncSample(Track track, double cutHere,boolean next) {double[] timeOfSyncSamples = new double[track.getSyncSamples().length];long currentSample = 0;double currentTime = 0;for (int i = 0; i < track.getSampleDurations().length; i++) {long delta = track.getSampleDurations()[i];if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) {timeOfSyncSamples[Arrays.binarySearch(track.getSyncSamples(),currentSample + 1)] = currentTime;}currentTime += (double) delta/ (double) track.getTrackMetaData().getTimescale();currentSample++;}double previous = 0;for (double timeOfSyncSample : timeOfSyncSamples) {if (timeOfSyncSample > cutHere) {if (next) {return timeOfSyncSample;} else {return previous;}}previous = timeOfSyncSample;}return timeOfSyncSamples[timeOfSyncSamples.length - 1];}}
【Android Studio使用mp4parser进行视频的分割】在博文结尾附上代码与jar包:地址