国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Java 合并多個MP4視頻文件

瀏覽:2日期:2022-08-20 18:37:06

局限性

只支持MP4文件經(jīng)過嘗試對于一些MP4文件分割不了

依賴

<!-- mp4文件操作jar --><!-- https://mvnrepository.com/artifact/com.googlecode.mp4parser/isoparser --><dependency><groupId>com.googlecode.mp4parser</groupId><artifactId>isoparser</artifactId><version>1.1.22</version></dependency>

工具類

package com.example.demo;import com.coremedia.iso.boxes.Container;import com.googlecode.mp4parser.authoring.Movie;import com.googlecode.mp4parser.authoring.Track;import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;import com.googlecode.mp4parser.authoring.tracks.AppendTrack;import com.googlecode.mp4parser.authoring.tracks.CroppedTrack;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.nio.channels.FileChannel;import java.util.ArrayList;import java.util.Arrays;import java.util.LinkedList;import java.util.List;public class Mp4ParserUtils { /** * 合并視頻 * * @param videoList: 所有視頻地址集合 * @param mergeVideoFile: 目標(biāo)文件 * @return */ public static String mergeVideo(List<String> videoList, File mergeVideoFile) { FileOutputStream fos = null; FileChannel fc = null; try { List<Movie> sourceMovies = new ArrayList<>(); for (String video : videoList) { sourceMovies.add(MovieCreator.build(video)); } List<Track> videoTracks = new LinkedList<>(); List<Track> audioTracks = new LinkedList<>(); for (Movie movie : sourceMovies) { for (Track track : movie.getTracks()) { if ('soun'.equals(track.getHandler())) { audioTracks.add(track); } if ('vide'.equals(track.getHandler())) { videoTracks.add(track); } } } Movie mergeMovie = new Movie(); if (audioTracks.size() > 0) { mergeMovie.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()]))); } if (videoTracks.size() > 0) { mergeMovie.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()]))); } Container out = new DefaultMp4Builder().build(mergeMovie); fos = new FileOutputStream(mergeVideoFile); fc = fos.getChannel(); out.writeContainer(fc); fc.close(); fos.close(); return mergeVideoFile.getAbsolutePath(); } catch (Exception e) { e.printStackTrace(); } finally { if (fc != null) { try { fc.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } /** * 剪切視頻 * @param srcVideoPath * @param dstVideoPath * @param times * @throws IOException */ public static void cutVideo(String srcVideoPath, String dstVideoPath, double[] times) throws IOException { int dstVideoNumber = times.length / 2; String[] dstVideoPathes = new String[dstVideoNumber]; for (int i = 0; i < dstVideoNumber; i++) { dstVideoPathes[i] = dstVideoPath + 'cutOutput-' + i + '.mp4'; } int timesCount = 0; for (int idst = 0; idst < dstVideoPathes.length; idst++) { //Movie movie = new MovieCreator().build(new RandomAccessFile('/home/sannies/suckerpunch-distantplanet_h1080p/suckerpunch-distantplanet_h1080p.mov', 'r').getChannel()); 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 CroppedTrack(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]; }}

以上就是Java 合并多個MP4視頻文件的詳細(xì)內(nèi)容,更多關(guān)于Java 合并視頻的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 亚洲成年人在线观看 | 狠狠色婷婷丁香综合久久韩国 | 久久亚洲国产精品一区二区 | 久久这里一区二区精品 | 99精品视频在线在线视频观看 | 日本成a人片在线观看网址 日本成年人视频网站 | 精品久久免费观看 | 新婚第一次一级毛片 | 久久国产精品视频 | 免费视频日本 | 精品日韩欧美一区二区三区在线播放 | 欧美亚洲综合网 | 久久精品一区 | 国产亚洲精品影达达兔 | 欧美日韩在线播一区二区三区 | 国产一区亚洲一区 | 日韩专区欧美 | 国产日韩亚洲欧美 | 在线视免费频观看韩国aaa | 一级做a爰片久久毛片鸭王 一级做a爰全过程免费视频毛片 | 九九99久久 | 国产舐足视频在线观看 | 在线不卡国产 | 手机在线色| 国产精品视频免费观看调教网 | 一级做a免费视频观看网站 一级做a爰 | 日本美女福利视频 | 99国产精品免费视频观看 | www.亚洲国产 | 亚洲系列中文字幕一区二区 | 午夜性爽快免费视频播放 | 日韩亚洲欧美综合一区二区三区 | 国产成人a在一区线观看高清 | 亚洲国产日韩欧美综合久久 | 亚洲久久在线观看 | 国产亚洲欧美日韩综合综合二区 | 欧美一做特黄毛片 | 波多野结衣中文一区二区免费 | 日韩欧美视频一区二区 | 久久国产欧美日韩高清专区 | 欧美ⅹxxxx视频 |