Java 合并多個(gè)MP4視頻文件
局限性
只支持MP4文件
經(jīng)過(guò)嘗試對(duì)于一些MP4文件分割不了
依賴(lài)
<!-- 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>
工具類(lèi)
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 合并多個(gè)MP4視頻文件的詳細(xì)內(nèi)容,更多關(guān)于Java 合并視頻的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
談?wù)凧ava中對(duì)象,類(lèi)和this,super,static關(guān)鍵字的使用
對(duì)象:對(duì)象是類(lèi)的一個(gè)實(shí)例,有狀態(tài)和行為。類(lèi):類(lèi)是一個(gè)模板,它描述一類(lèi)對(duì)象的行為和狀態(tài)。本文就來(lái)和大家聊聊Java中對(duì)象,類(lèi)和關(guān)鍵字的使用,需要的可以參考一下2022-08-08Java 類(lèi)與對(duì)象重難點(diǎn)詳解
類(lèi)(class)和對(duì)象(object)是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語(yǔ)言的合稱(chēng)。對(duì)象是對(duì)客觀事物的抽象,類(lèi)是對(duì)對(duì)象的抽象。類(lèi)是一種抽象的數(shù)據(jù)類(lèi)型2021-11-11Nacos框架服務(wù)注冊(cè)實(shí)現(xiàn)流程
這篇文章主要介紹了SpringCloud服務(wù)注冊(cè)之nacos實(shí)現(xiàn)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08SpringBoot?2.5.5整合輕量級(jí)的分布式日志標(biāo)記追蹤神器TLog的詳細(xì)過(guò)程
分布式追蹤系統(tǒng)是一個(gè)最終的解決方案,如果您的公司已經(jīng)上了分布式追蹤系統(tǒng),這篇文章主要介紹了SpringBoot?2.5.5整合輕量級(jí)的分布式日志標(biāo)記追蹤神器TLog,需要的朋友可以參考下2022-10-10Prometheus 入門(mén)教程之SpringBoot 實(shí)現(xiàn)自定義指標(biāo)監(jiān)控
這篇文章主要介紹了Prometheus 入門(mén)教程之SpringBoot 實(shí)現(xiàn)自定義指標(biāo)監(jiān)控,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Java編程思想里的泛型實(shí)現(xiàn)一個(gè)堆棧類(lèi) 分享
這篇文章介紹了Java編程思想里的泛型實(shí)現(xiàn)一個(gè)堆棧類(lèi),有需要的朋友可以參考一下2013-07-07SpringBoot WebSocket實(shí)時(shí)監(jiān)控異常的詳細(xì)流程
最近做了一個(gè)需求,消防的設(shè)備巡檢,如果巡檢發(fā)現(xiàn)異常,通過(guò)手機(jī)端提交,后臺(tái)的實(shí)時(shí)監(jiān)控頁(yè)面實(shí)時(shí)獲取到該設(shè)備的信息及位置,然后安排員工去處理。這篇文章主要介紹了SpringBoot WebSocket實(shí)時(shí)監(jiān)控異常的全過(guò)程,感興趣的朋友一起看看吧2021-10-10Java實(shí)現(xiàn)數(shù)字轉(zhuǎn)成英文的方法
這篇文章主要介紹了Java實(shí)現(xiàn)數(shù)字轉(zhuǎn)成英文的方法,涉及java數(shù)組與字符串的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05