我正在尝试修剪或剪切特定时间的视频,例如:- Video-1 是 30 秒修剪到 Video-2 10 秒(0.10 秒到 0.20 秒)。我能够做到这一点并且能够播放这个视频,但在视频结束时它给出了错误提示 -Sorry! cant play this video.
public static void main(String args) throws IOException {
Movie movie = new MovieCreator()
.build(new RandomAccessFileIsoBufferWrapperImpl(
new File(
"/sdcard/Videos11/"+args+".mp4")));
List<Track> tracks = movie.getTracks();
movie.setTracks(new LinkedList<Track>());
// remove all tracks we will create new tracks from the old
double startTime = 3.000;
double endTime = 9.000;
boolean timeCorrected = false;
// Here we try to find a track that has sync samples. Since we can only
// start decoding
// at such a sample we SHOULD make sure that the start of the new
// fragment is exactly
// such a frame
for (Track track : tracks) {
if (track.getSyncSamples() != null
&& track.getSyncSamples().length > 0) {
if (timeCorrected) {
// This exception here could be a false positive in case we
// have multiple tracks
// with sync samples at exactly the same positions. E.g. a
// single movie containing
// multiple qualities of the same video (Microsoft Smooth
// Streaming file)
throw new RuntimeException(
"The startTime has already been corrected by another track with SyncSample. Not Supported.");
}
}
for (Track track : tracks) {
long currentSample = 0;
double currentTime = 0;
long startSample = -1;
long endSample = -1;
for (int i = 0; i < track.getDecodingTimeEntries().size(); i++) {
TimeToSampleBox.Entry entry = track.getDecodingTimeEntries().get(i);
for (int j = 0; j < entry.getCount(); j++) {
// entry.getDelta() is the amount of time the current sample
// covers.
if (currentTime <= startTime) {
// current sample is still before the new starttime
startSample = currentSample;
}
if (currentTime <= endTime) {
// current sample is after the new start time and still
// before the new endtime
endSample = currentSample;
} else {
// current sample is after the end of the cropped video
break;
}
currentTime += (double) entry.getDelta()
/ (double) track.getTrackMetaData().getTimescale();
currentSample++;
}
}
movie.addTrack(new CroppedTrack(track, startSample, endSample));
}
IsoFile out = new DefaultMp4Builder().build(movie);
String filePath = "sdcard/test"+i+".mp4";
i++;
File f = new File(filePath);
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos, 65535);
out.getBox(new IsoOutputStream(bos));
bos.close();
fos.close();
}
PS:-我对这段代码不太熟悉,但有些如何能够修剪视频,但最后显示错误。