Java合并多个MP4视频⽂件转载: ⽤于读取、写⼊和创建MP4⽂件的Java API
注意编码格式
局限性
只⽀持MP4⽂件
经过尝试对于⼀些MP4⽂件分割不了
功能⽐较少
⽬前点评有采⽤这种⽅案做视频的合并
以下代码都是转载⼤佬案例的代码:
⼯具类:
ample.demo;
dia.iso.boxes.Container;
lecode.mp4parser.authoring.Movie;
lecode.mp4parser.authoring.Track;
lecode.mp4parser.authoring.builder.DefaultMp4Builder;
lecode.ainer.mp4.MovieCreator;
lecode.acks.AppendTrack;
lecode.acks.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 : Tracks()) {
if ("soun".Handler())) {
audioTracks.add(track);
}
if ("vide".Handler())) {
videoTracks.add(track);
}
}
}
Movie mergeMovie = new Movie();
if (audioTracks.size() > 0) {
mergeMovie.addTrack(new Array(new Track[audioTracks.size()])));
}
if (videoTracks.size() > 0) {
mergeMovie.addTrack(new Array(new Track[videoTracks.size()])));
}
Container out = new DefaultMp4Builder().build(mergeMovie);
fos = new FileOutputStream(mergeVideoFile);
fc = Channel();
out.writeContainer(fc);
fc.close();
fos.close();
AbsolutePath();
} 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/v", "r").getChannel());            Movie movie = MovieCreator.build(srcVideoPath);
List<Track> tracks = Tracks();
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 (SyncSamples() != null && SyncSamples().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 < SampleDurations().length; i++) {
long delta = SampleDurations()[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) TrackMetaData().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 = Channel();
out.writeContainer(fc);
fc.close();
fos.close();
long start3 = System.currentTimeMillis();
}
java创建文件}
private static double correctTimeToSyncSample(Track track, double cutHere, boolean next) {
double[] timeOfSyncSamples = new SyncSamples().length];
long currentSample = 0;
double currentTime = 0;
for (int i = 0; i < SampleDurations().length; i++) {
long delta = SampleDurations()[i];
if (Arrays.SyncSamples(), currentSample + 1) >= 0) {
// samples always start with 1 but we start with zero therefore +1
timeOfSyncSamples[Arrays.SyncSamples(), currentSample + 1)] = currentTime;
}
currentTime += (double) delta / (double) TrackMetaData().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];
}
}
Mp4ParserUtils.java
2019-12-09发现问题: 没有解决...求⼤佬⽀招
浏览器录屏的mp4⽂件合并时出现程序⼀直运⾏,然后⽆法处理,main⽅法没有主动关闭
ffmpeg⼯具合并也有问题:(ffmpeg命令转ts⽂件正常)但是合并所有的ts⽂件时报错:
Codec 'mp3' (86017) is not supported by the bitstream filter 'aac_adtstoasc'. Supported codecs are: aac (86018)
Error initializing bitstream filter: aac_adtstoasc
⽐特流过滤器'aac_adtstoasc'不⽀持编解码器'mp3'(86017)。⽀持的编解码器为:AAC(86018)
初始化位流过滤器时出错:aac_adtstoasc

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。