視頻裁剪沒聲音?解決視頻裁剪沒聲音問題,實現快速裁剪,只需裁剪時間,文件源,一切簡單

這陣子項目上線了,但是更大的麻煩來了。。那就是要做視頻處理啊。。。。先來解決視頻裁剪吧,之前有看過一些視頻裁剪的,但是基本上都是沒用的,但是還是有了思路,那就是用google推出的mp4parser,(其實我比較嚮往用ffmpeg來做任何視頻的處理,但是底子不夠,不會那些底層啊),查閱源碼後發現還是比較簡單的,但是有個缺陷,只可以搞mp4視頻格式啊,我的內心是一萬個曹尼瑪啊。。。。不多說上源碼:

import org.mp4parser.Container;
import org.mp4parser.muxer.Movie;
import org.mp4parser.muxer.Track;
import org.mp4parser.muxer.builder.DefaultMp4Builder;
import org.mp4parser.muxer.container.mp4.MovieCreator;
import org.mp4parser.muxer.tracks.ClippedTrack;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.sql.Time;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class ShortenVideo {
    public static void videoCut(String srcVideoPath, String dstVideoPath, String name, double[] times) throws IOException {
        int dstVideoNumber = times.length / 2;

        Calendar calendar = Calendar.getInstance();
        calendar.get(Calendar.HOUR_OF_DAY);
        calendar.get(Calendar.MINUTE);

        String[] dstVideoPathes = new String[dstVideoNumber];

        long time = System.currentTimeMillis();
        Date date = new Date(time);
        java.text.SimpleDateFormat sDateFormat = new java.text.SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");

        for (int i = 0; i < dstVideoNumber; i++) {
            dstVideoPathes[i] = dstVideoPath + "/" + "cutMovie-" + sDateFormat.format(date) + "'\n’" + name;
        }
        int timesCount = 0;

        for (int idst = 0; idst < dstVideoPathes.length; idst++) {

            Movie movie = MovieCreator.build(srcVideoPath);

            List<Track> tracks = movie.getTracks();
            movie.setTracks(new LinkedList<Track>());
            // remove all tracks we will create new tracks from the old
            double startTime1 = times[timesCount];
            double endTime1 = times[timesCount + 1];
            timesCount = timesCount + 2;
            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.");
                    }
                    startTime1 = correctTimeToSyncSample(track, startTime1, false);
                    endTime1 = correctTimeToSyncSample(track, endTime1, 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 <= startTime1) {
                        // current sample is still before the new starttime
                        startSample1 = currentSample;
                    }
                    if (currentTime > lastTime && currentTime <= endTime1) {
                        // current sample is after the new start time and still before the new endtime
                        endSample1 = currentSample;
                    }

                    lastTime = currentTime;
                    currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
                    currentSample++;
                }
                //movie.addTrack(new AppendTrack(new ClippedTrack(track, startSample1, endSample1), new ClippedTrack(track, startSample2, endSample2)));
                movie.addTrack(new ClippedTrack(track, startSample1, endSample1));
            }
            long start1 = System.currentTimeMillis();
            Container out = new DefaultMp4Builder().build(movie);

            long start2 = System.currentTimeMillis();
            FileOutputStream fos = new FileOutputStream(String.format(dstVideoPathes[idst]));
            FileChannel fc = fos.getChannel();
            out.writeContainer(fc);

            fc.close();
            fos.close();
            long start3 = System.currentTimeMillis();

        }
    }

    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.getSampleDurations().length; i++) {
            long delta = track.getSampleDurations()[i];

            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) 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];
    }
}
這是我修改過了的源碼,之前看別人的代碼是這樣的,點擊剪切視頻,隨機生成3端視頻。。。第二次裁剪會覆蓋之前的視頻裁剪,所以我改了下。現在是你輸入開始裁剪時間和結束裁剪時間,就OK了,也不用擔心覆蓋問題,之所以覆蓋是因爲文件名重複了,所以我加了一個時間作爲標識,這樣就O了。到現在還沒扯到標題上面,,那就是剪切後沒聲音,原因很簡單,導入的包不對,這個東西不同的包差別有點大。。。這樣就哦了,上面是個工具類,可以直接用,如果有什麼疑問可以在下面的評論留言哦,最近我一直在留意博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章