使用JAVA将m3u8转换为mp4格式

0 篇文章 0 订阅

今天下载了一部电影,不过是m3u8格式,想要分享分享不了,就写了个小程序将m3u8转换为mp4

代码的使用方法

1.下载代码,链接如下

编译好的代码地址

2.将下载好的代码复制至m3u8文件夹

3.打开文件夹,在地址栏输入cmd,按回车(Enter键),见下图

4.在命令窗口输入java M3u8ToMP4,即可在当前文件夹下看到转换后的文件

如果不想下载,可以使用下面代码自己进行编译,源码如下


import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * M3u8转MP4工具类
 * @author nzmzx
 */
public class M3u8ToMP4 {

	public static void main(String[] args) throws IOException {
		String property = System.getProperty("user.dir");
		File file = new File(property);// 文件夹
		String runFilePath = property + "/new.bat";
		File runFile = new File(runFilePath);// 生成的可执行文件
		FileWriter fw = null;
		try {
			fw = new FileWriter(runFile);
			fw.append("copy /b ");
			fw.flush();
			if (file.exists()) {// 1.判断文件是否存在
				if (file.isDirectory()) {// 2.判断是否为文件夹
					File[] listFiles = file.listFiles();
					List<String> sortList = new ArrayList<String>();
					for (File file2 : listFiles) {
						String name = file2.getName();// 获取名字
						if(!(name.endsWith(".class")||name.endsWith(".bat"))) 
							sortList.add(name);
					}
					//4.排序
					sortList.sort((o1,o2)->{
						String split1 = o1.split("\\.")[0];
						String split2 = o2.split("\\.")[0];
						int parseInt1;
						int parseInt2;
						try {
							parseInt1 = Integer.parseInt(split1);
							parseInt2 = Integer.parseInt(split2);
						} catch (NumberFormatException e) {//如果是非数字,按照原来的顺序
							return 0 ;
						}
						if(parseInt1>parseInt2) {
							return 1;
						}else {
							return -1;
						}
					});
					int size = sortList.size();
					for (int i = 0; i < size; i++) {
						System.out.println(sortList.get(i));
						if (i == size - 1) {
							fw.append(sortList.get(i));
							break;
						}
						fw.append(sortList.get(i) + "+");
						fw.flush();
					}
					fw.append(" new.mp4");
					fw.flush();
					fw.close();
				}
			}
			callCmd(runFilePath);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fw != null) {
				fw.close();
			}
		}
	}

	private static void callCmd(String locationCmd) {
		StringBuilder sb = new StringBuilder();
		InputStream in = null;
		BufferedReader bufferedReader = null;
		try {
			Process child = Runtime.getRuntime().exec(locationCmd);
			in = child.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(in));
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				sb.append(line + "\n");
			}
			bufferedReader.close();
			in.close();
			try {
				child.waitFor();
			} catch (InterruptedException e) {
				System.out.println(e);
			}
			System.out.println("sb:" + sb.toString());
			System.out.println("callCmd execute finished");
			//执行完成后,将执行文件删除。
			File file=new File(locationCmd);
			file.delete();
		} catch (IOException e) {
			System.out.println(e);
		} finally {
			try {
				//将流关闭,从小往大关,防止流未关闭占用内存
				if (bufferedReader != null) {
					bufferedReader.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
			}
		}
	}

}

注意:使用命令需要有java环境!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值