java-使用mp4parser剪切mp4.无法过去时间与SyncSample校正

java-使用mp4parser剪切mp4.无法过去时间与SyncSample校正,第1张

概述我正在编写一个Android应用程序,并且有很多5-15秒的.mp4文件要剪辑.我一直在尝试使用塞巴斯蒂安·安妮斯(SebastianAnnies)的mp4parser,遵循此处给出的示例代码:ShortenExample.这是我正在使用的代码(与上面的示例代码非常相似):publicstaticvoidclip(Sprinklesprinkle,

我正在编写一个Android应用程序,并且有很多5-15秒的.mp4文件要剪辑.我一直在尝试使用塞巴斯蒂安·安妮斯(Sebastian AnnIEs)的mp4parser,遵循此处给出的示例代码:ShortenExample.

这是我正在使用的代码(与上面的示例代码非常相似):

    public static voID clip(Sprinkle sprinkle, double start, double end) throws IOException {    MovIE movIE = MovIECreator.build(sprinkle.getLocalVIDeoPath());    // Save all tracks then remove them from movIE    List<Track> tracks = movIE.getTracks();    movIE.setTracks(new linkedList<Track>());    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)                Log.v("clip", "track.getSyncSamples().length: " + track.getSyncSamples().length);                throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");            }            Log.v("syncSample", "start before: " + start);            Log.v("syncSample", "end before: " + end);            start = correctTimetoSyncSample(track, start, false);            end = correctTimetoSyncSample(track, end, true);            Log.v("syncSample", "start after: " + start);            Log.v("syncSample", "end after: " + end);            timeCorrected = true;        }    }    for (Track track : tracks) {        long currentSample = 0;        double currentTime = 0;        double lastTime = 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++) {                if (currentTime > lastTime && currentTime <= start) {                    // current sample is still before the new starttime                    startSample = currentSample;                }                if (currentTime > lastTime && currentTime <= end) {                    // current sample is after the new start time and still before the new endtime                    endSample = currentSample;                }                lastTime = currentTime;                currentTime += (double) entry.getDelta() / (double) track.getTrackMetaData().getTimescale();                currentSample++;            }        }        movIE.addTrack(new AppendTrack(new CroppedTrack(track, startSample, endSample)));    }    long start1 = System.currentTimeMillis();    Container out = new DefaultMp4Builder().build(movIE);    long start2 = System.currentTimeMillis();    file file = Constants.getEditsDir();    fileOutputStream fos = new fileOutputStream(file.getPath() + String.format("output-%f-%f.mp4", start, end));    fileChannel fc = fos.getChannel();    out.writeContainer(fc);    fc.close();    fos.close();    long start3 = System.currentTimeMillis();    System.err.println("Building Isofile took : " + (start2 - start1) + "ms");    System.err.println("Writing Isofile took  : " + (start3 - start2) + "ms");    System.err.println("Writing Isofile speed : " + (new file(String.format("output-%f-%f.mp4", start, end)).length() / (start3 - start2) / 1000) + "MB/s");}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 < track.getDeCodingTimeEntrIEs().size(); i++) {        TimetoSampleBox.Entry entry = track.getDeCodingTimeEntrIEs().get(i);        for (int j = 0; j < entry.getCount(); j++) {            if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) {                // samples always start with 1 but we start with zero therefore +1                timeOfSyncSamples[Arrays.binarySearch(track.getSyncSamples(), currentSample + 1)] = currentTime;            }            currentTime += (double) entry.getDelta() / (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];}

我似乎无法防止出现错误“ startTime已被SyncSample的其他轨道纠正.不支持.”从发生.当我记录正在遍历的轨道时,getHandler()返回“ vIDe”,“ soun”,然后在涉及“提示”时崩溃.如果我将这一部分注释掉:

    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)            Log.v("clip", "track.getSyncSamples().length: " + track.getSyncSamples().length);            throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");        }

然后程序进入该行时,由于索引超出范围错误而崩溃

    Container out = new DefaultMp4Builder().build(movIE);

我究竟做错了什么?

解决方法:

您收到indexoutofboundsexception,因为到达时startSample或endSample的值错误(例如,仍为-1)

Container out = new DefaultMp4Builder().build(movIE);

在我的示例中,对于小于2秒的剪辑过程使用起始值会导致以下结果:if(currentTime> lastTime&& currentTime< = start)从未达到真实值,因此startSample并未从其更新初始值为-1.一种解决方案是将startSample的初始值从-1更改为0.

总结

以上是内存溢出为你收集整理的java-使用mp4parser剪切mp4.无法过去时间与SyncSample校正全部内容,希望文章能够帮你解决java-使用mp4parser剪切mp4.无法过去时间与SyncSample校正所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1087698.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存