gpt4 book ai didi

android - 在 mp4Parser 的帮助下修剪视频

转载 作者:太空狗 更新时间:2023-10-29 14:11:58 26 4
gpt4 key购买 nike

我正在尝试针对特定时间修剪或剪切视频,示例:- Video-1 是 30 秒修剪到 Video-2 10 秒(0.10 秒到 0.20 秒)。我能够执行此操作并能够播放该视频,但在视频结尾时出现错误消息 - Sorry!无法播放此视频。

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();

}

P.S:-我不太熟悉这段代码,但知道如何修剪视频,但它最后显示错误。

最佳答案

您应该“规范化”您的开始和停止值,使其与 mp4 同步样本相对应,否则您会在输出视频上出现一些故障,甚至损坏文件。

请查看correctTimeToSyncSample 方法:

  1. Android Gallery source (我猜使用 mp4parser 0.9.x)
  2. My sample project (使用 mp4parser 1.1.18)。
private 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 = 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];
}

用法:

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.");
}
startTime = correctTimeToSyncSample(track, startTime, false);
endTime = correctTimeToSyncSample(track, endTime, true);
timeCorrected = true;
}
}

关于android - 在 mp4Parser 的帮助下修剪视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26841622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com