java 合并视频_Java 合并多个MP4视频文件
局限性
只支持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视频文件相关推荐
- vep文件如何转换mp4_如何将m4v视频格式快速转换成mp4视频呢
如今我们的数码相机,手机,摄像机越来越丰富了,拍摄的视频也越来越多了有的文件也是比较大,如何去转换为其它的格式呢还有就是如果你的视频制作完成后如何去转换为其它的格式放到网站上呢今天就给大家操作一下视频 ...
- 如何将m4v视频格式快速转换成mp4视频呢
如今我们的数码相机,手机,摄像机越来越丰富了,拍摄的视频也越来越多了有的文件也是比较大,如何去转换为其它的格式呢还有就是如果你的视频制作完成后如何去转换为其它的格式放到网站上呢今天就给大家操作一下视频 ...
- java gzip压缩_Java GZIP示例–压缩和解压缩文件
java gzip压缩 Welcome to Java GZIP example. GZIP is one of the favorite tool to compress file in Unix ...
- java代码整合_java合并多个文件的实例代码
在实际项目中,在处理较大的文件时,常常将文件拆分为多个子文件进行处理,最后再合并这些子文件.下面就为各位介绍下Java中合并多个文件的方法. Java中合并子文件最容易想到的就是利用BufferedS ...
- java 输入流可以合并吗_Java 使用IO流实现大文件的分割与合并实例详解
java 使用IO流实现大文件的分割与合并 文件分割应该算一个比较实用的功能,举例子说明吧比如说:你有一个3G的文件要从一台电脑Copy到另一台电脑, 但是你的存储设备(比如SD卡)只有1G ,这个时 ...
- Java多个ppt合并脚本_Java 合并、拆分PPT幻灯片
随着PPT文档在日常工作中的使用越来越频繁,为了便于操作和管理文档,时常会遇到需要将PPT幻灯片进行合并或拆分的情况.一般来说,合并包括将指定幻灯片合并到文档.将多个幻灯片文档合并为一个文档:拆分包括 ...
- java二进制视频_Java二进制概念(含视频)
我们平时认识的数字比如1.2.3.4等数字叫做十进制数字,我们可以看懂,但是计算机无法运算,如果计算机要计算这些数字就得将这些数字转换成计算机能读懂的数据,计算只能读懂二进制数字,二进制的数字有什么特 ...
- java mp4 视频时间戳_如何在MP4视频文件上批量修改时间戳记元数据
基础上QuickTime File Format Specification我放在一起的时间戳的原油转移证明了概念的Python脚本,让我用现在: #!/usr/bin/env python impo ...
- java程序设计教程视频_Java程序设计标准教程:DVD视频教学版
第1章 搭建Java开发环境. 001 1.1 Java语言的产生与发展 002 1.2 Java语言的特点 002 1.3 搭建Java开发环境 003 1.3.1 下载JDK 003 1.3.2 ...
最新文章
- 强势的老板--项目管理
- ACL 2021 | Glancing Transformer:惊鸿一瞥的并行生成模型
- opencv图像清晰度计算_收藏|分析君带你认识Python中的十大图像处理工具
- kotlin学习目录
- Go语言来了,要代替C和Python?
- aws eks_在生产中配置和使用AWS EKS
- 对话阿里云MVP裔隽跨界半生,不改赤子心
- rsync同步数据到内网
- github的python代码怎么跑_如何利用Python模拟GitHub登录详解
- “才子进销存”新一代真正基于互联网(Internet)的进销存分销管理软件
- JAVA SE、JAVA EE、JAVA ME的联系与区别
- 2021-07-04应用的生命周期
- python turtle原点位置_python中turtle库中setworldcoordinates(坐标系的移动)
- Unity实现多旋翼无人机的模拟飞行(物理引擎)
- python自动提交网页表单_Python 自动化表单提交实例代码
- OpenStack HA
- 编程游戏开发【飞翔的小鸟】
- php微信支付宝第三方接口开发平台,帝国CMS第三方个人支付接口微信支付宝免签约即时到账api_帝国网站管理系统插件...
- .NET WebApi 实战第三讲
- dubbo优点是什么dubbo有哪些缺点