Java 合并多个MP4视频文件
作者:泡沫幻影 发布时间:2022-08-23 12:09:19
标签:Java,合并,视频,MP4
局限性
只支持MP4文件
经过尝试对于一些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: 目标文件
* @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];
}
}
来源:https://www.cnblogs.com/yxgmagic/p/11983149.html


猜你喜欢
- 在textView添加超链接,有两种方式,第一种通过HTML格式化你的网址,一种是设置autolink,让系统自动识别超链接。 代码如下:
- 本文介绍了浅谈Java的两种多线程实现方式,分享给大家。具有如下:一、创建多线程的两种方式Java中,有两种方式可以创建多线程:1 通过继承
- 活锁与死锁活锁活锁同样会发生在多个相互协作的线程间,当他们为了彼此间的响应而相互礼让,使得没有一个线程能够继续前进,那么就发生了活锁。同死锁
- Timer 详解Timer 和 TimerTask 用于在后台线程中调度任务的 java.
- 本文实例讲述了C#通过链表实现队列的方法。分享给大家供大家参考。具体实现方法如下:public class Node{ public int
- 一、工具类代码public class TaskHelper {#region 多线程操作 &nbs
- 前言在阅读本文之前, 希望你可以思考一下下面几个问题, 带着问题去阅读文章会获得更好的效果。发送消息的时候, 当Broker挂掉了,消息体还
- mapper.xml使用循环语句mapper.java,传的参数是mapList<实体类> getList(Map<Str
- Spring 中bean的获取1.通过context.getbean 的方式来获取beanApplicationContext:是sprin
- 提供表示 Windows 注册表中的根项的 RegistryKey 对象,并提供访问项/值对的 static&
- 客户端的代码:package tcp.http;import java.io.*;import java.net.*;import java
- import java.io.BufferedInputStream;import java.util.ArrayList;import j
- 前言最近在学习C# Socket相关的知识,学习之余,动手做了一个简单的局域网聊天器。有萌生做这个的想法,主要是由于之前家里两台电脑之间想要
- 把char数组转换成String调用reverseStr()传入一个字符串"let’s"
- Spring Boot FeignClient 捕获业务异常信息因项目重构采用spring cloud,feign不可避免。目前spring
- 窗口是GUI编程的基础,小应用程序或图形界面的应用程序的可视组件都放在窗口中,在GUI中,窗口是用户屏幕的一部分,起着在屏幕中一个小屏幕的作
- 前两天,谷歌发布了Android Studio 1.0的正式版,也有更多的人开始迁移到Android Studio进行开发。然而,网上很多的
- 本文实例为大家分享了Android中TabLayout结合ViewPager实现页面切换,供大家参考,具体内容如下一、实现思路1、在buil
- Spring的事务隔离级别和事务的传播行为是面试中经常考察的问题,做个简单的总结。传播行为在SpringBoot中通过Transaction
- Monkey 是Android SDK提供的一个命令行工具, 可以简单,方便地运行在任何版本的Android模拟器和实体设备上。 Monke