局限性

只支持MP4文件

经过尝试对于一些MP4文件分割不了

依赖

com.googlecode.mp4parser

isoparser

1.1.22

工具类

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 videoList, File mergeVideoFile) {

FileOutputStream fos = null;

FileChannel fc = null;

try {

List sourceMovies = new ArrayList<>();

for (String video : videoList) {

sourceMovies.add(MovieCreator.build(video));

}

List videoTracks = new LinkedList<>();

List 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 tracks = movie.getTracks();

movie.setTracks(new LinkedList());

// 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视频文件的详细内容,更多关于Java 合并视频的资料请关注聚米学院其它相关文章!

java 视频 合并成一个_Java 合并多个MP4视频文件相关推荐

  1. 如何将多个excel表格合并成一个_多个PDF如何合并成一个?就用这个PDF在线工具!...

    多个PDF如何合并成一个?现在网上下载的资料文件大都是PDF格式的,且下载后文件都被分成好多部分.想要使用起来更加方便,需要先将这些分散的PDF文件合并起来.我们怎么操作才能将多个PDF文件合并成一个 ...

  2. 推荐几种方法把两个pdf合并成一个pdf

    把两个pdf合并成一个pdf?将两个PDF文件合并成一个PDF文件是一种非常常见的操作,尤其在工作和生活中需要处理大量信息和文件时.将两个PDF文件合并为一个PDF文件可以更便捷地管理和查看文件.如果 ...

  3. java jar合并_多个jar包合并成一个jar包(ant)

    https://blog.csdn.net/gzl003csdn/article/details/53539133 多个jar包合并成一个jar 使用Apache的Ant是一个基于Java的生成工具. ...

  4. 怎么把几个视频合并成一个?学会这个技巧,轻松掌握

    你们在闲暇时是不是经常通过刷短视频来消磨时间呢?那有没有刷到过一些鬼畜的搞笑视频,它们的制作方式就是把多个视频片段合并到一起,将毫无关联的内容.剧情进行梦幻联动,例如各种跨界cp.精彩瞬间等,直击当代 ...

  5. 怎么让两个java文件关联,怎么把多个excel文件合并成一个【几个excle合并成一个】...

    怎么把多个excel文件合并成一个 新建一个BOOK文件,按Alt F11这二个键,VBA编辑界面,点击编辑的菜单:"插入"--"",然后在里面完整复制下面的代 ...

  6. Java使用iText PDF合并PDF(将多个PDF合并成一个PDF)

    1 配置pom文件 我用的是5.4.3的版本 <dependency><groupId>com.itextpdf</groupId><artifactId&g ...

  7. java实现多个mav文件拼接合并成一个mav文件

    java实现多个mav文件拼接合并成一个mav文件,绝对有效 解决方法: import java.io.File; import java.io.IOException; import java.io ...

  8. 如何把两段即以上视频合并成一个

    随着科技的快速发展,我们使用视频和图片的机会越来越多,快节奏的生活方式,使得很多人对文字失去了耐心,不管是学习或者是简单的娱乐,都会用到视频,像小编一样有自己的爱豆的会经常把很多视频合并成一个,这样很 ...

  9. java jdk8 使用stream实现两个list集合合并成一个list集合(对象属性的合并)

    java使用stream实现list中对象属性的合并: 根据两个List中的某个相同字段合并成一条List,包含两个List中的字段 目录 一.前言 二.示例 示例1:java8 合并两个 list& ...

最新文章

  1. “三电一兽”竞争格局将打破,网络营销外包下搜电开启高效运营
  2. nyoj--364--田忌赛马(贪心)
  3. 安装rlwrap 的简单方法,亲测好用
  4. 联想笔记本暗屏几乎看不见_2020年内存条推荐-选购指南(DDR3/DDR4/台式/笔记本内存)...
  5. ElasticSearch5.0——IK词库加载
  6. 自学软件测试需要学到哪些内容?
  7. Ionic3.x 创建项目中的问题-IonIC start myApp tabs
  8. 缔造企鹅:产品经理是这样炼成的札记-技巧
  9. 一个小游戏的代码(猜数字)
  10. 润乾报表入门问题记录
  11. 我跳过的坑-解决linux的输入法问题。
  12. 可口可乐市场调查失败的原因_经典案例5:可口可乐:一次市场调研失败的教训...
  13. linux查看指定目录下各个文件大小以及总体大小
  14. 百慕大群岛计划发起区块链土地登记系统
  15. PAT 乙级 1070  结绳
  16. C语言实现四元数的乘法(三维矢量、四元数以及旋转矢量与四元数相乘源码)
  17. python 与and运算符
  18. 芝加哥大学计算机语言学,芝加哥大学cs专业值得申请么?
  19. EarlyStopping
  20. 嘉明SSM学习之基于SSM框架的学生信息管理系统

热门文章

  1. 为什么bagging降低方差,boosting降低偏差?
  2. Android代码关闭过渡动画,关闭Android过度缩放动画
  3. 二分图——洛谷P1155 双栈排序
  4. 2017年,毫无作为的感想篇
  5. 应用预测建模第四章过度拟合与模型调优习题4.4【分层随机抽样、小样本的模型评估方案】
  6. 北京突然宣布,元宇宙重大消息
  7. 学校图书馆计算机书籍,学校图书馆共有 300 万册图书,想统计其中 Compute
  8. css阴影怎么添加,css如何为div添加阴影效果
  9. html5页面嵌入pdf,有没有办法将pdf文件嵌入到html5页面?
  10. python爬虫英文翻译_Python爬虫实现翻译功能