Java-数字编号的M3U8文件集合并为一个视频

数字编号的M3U8文件集合并为一个视频

介绍

文件本身其实使用播放器就可以播放,所以,目标就是将这些文件合并成一个文件。对文件进行排序拼接即可

代码

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;


/**  
 * @Title: M3U8ToVideo.java
 * @author ihan
 * @date 2021-12-02 09:53:03 
 * @Description: TODO(描述)
 */

public class M3U8ToVideo {
	/**
	 * 合并文件
	 * @param from 源目录
	 * @param to 目标目录
	 * @param fileName 文件名
	 */
	public static boolean mergeFiles(String from, String to, String fileName) {
		File fromDir = new File(from);
		if (!fromDir.exists()) {
			System.out.println("源目录不存在!");
			return false;
		}
		File toPath = new File(to);
		if (!toPath.exists()) {
			toPath.mkdirs();
		}
		File toFile = new File(to + "/" + fileName);
		if (!toFile.exists()) { // 判断文件是否存在
			try {
				toFile.createNewFile(); // 创建文件夹
				toFile.mkdirs(); // 创建空文件
				List<File> files = Arrays.asList(fromDir.listFiles());
				Pattern pattern = Pattern.compile("^[0-9]*$"); 
				for(File f : files) { // 遍历文件
					if (f.isFile() && !pattern.matcher(f.getName()).matches()) {
						f.delete();
					}
				}
				Collections.sort(files, new MyComparator());
				Collections.sort(files, new MyComparator()); // 只执行一次的时候会出现一些问题
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(toFile));
				for(File f : files) { // 遍历文件
					if (f.isFile()) {
						BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
						bos.write(bis.readAllBytes());
						bis.close();
					}
				}
				bos.flush();
				bos.close();
				return true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}else {
			System.out.println("文件已经存在,请重命名或者输入不同的名字。");
			return false;
		}
	}
	
	public static void main(String[] args) {
		String fromPath = args[0]; // 文件所在目录
		String toPath = args[1].equals("./") ? fromPath : args[1]; // 是否是当前目录
		String suffix = args[3];
		String fileName = (null == args[2] ? "合并视频" : args[2]) + suffix;
		boolean flag = mergeFiles(fromPath, toPath, fileName);
		if (flag) {
			System.out.println("合并成功");
		}else {
			System.out.println("合并失败!");
		}
	}
}

/**
 * 自定义比较器
 * @author DELL
 * @date 2021-12-02 11:12:39
 */
class MyComparator implements Comparator<File>{
	@Override
	public int compare(File o1, File o2) {
		// TODO Auto-generated method stub
		int o1Name = 0;
		int o2Name = 0;
		try {
			o1Name = Integer.parseInt(o1.getName());
			o2Name = Integer.parseInt(o2.getName());
		} catch (Exception e) {
			// TODO: handle exception
			return -1;
		}
		if (o1Name > o2Name) {
			return 1;
		}else if (o1Name < o2Name) {
			return -1;
		}
		return 0;
	}
}

使用

参数有三个

  • M3U8文件夹路径,绝对路径
  • 合并文件的保存路径,可以不存在,不存在则会自动创建
  • 合并文件的文件名,不要加后缀
  • 合并文件的后缀名,例如“.mp4”,需要加‘.’

使用示例

javac -encoding UTF8 M3U8ToVideo.java
java M3U8ToVideo "D:/m3u8/" "D:/res" "合并视频" ".mp4"

最终会生成D:/res/合并视频.mp4

遇见的问题

  1. javac编译时,提示未识别的GBK字符
    -encoding UTF8 需要手动指定字符集
  2. 加载不到主类
    代码中不要加package包路径。加包路径的,需要再编译和运行时加上包路径。具体操作自行搜索。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值