|
|
本帖最后由 焱妃 于 2023-7-25 10:55 编辑
想必大家都有看过喜欢的电影想下载吧,但抓包发现是m3u8文件,通过解析下载了所有ts视频,但却没有相应的(手机)软件进行合并(电脑可以使用copy /b *.ts 1.m4来实现),这里我用C/Java实现了合并ts视频的功能
必须读取m3u8文件,不要问我为啥不直接listfile()列出所有文件,因为这样列出的文件顺序是乱的
不要照搬,不要照搬,每个m3u8文件都不一样,得根据你的m3u8文件来看




- import java.io.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.*;
- public class Main {
- static List<String> list = new ArrayList<>();
- public static void main(String[] args) {
- String txt;
- boolean ok=true;
- System.out.println("输入m3u8文件路径");
- Scanner sc = new Scanner(System.in);
- File f = new File(sc.next());
- System.out.println("输入导出的文件名");
- String dst = sc.next();
- try{
- BufferedReader br = new BufferedReader(new FileReader(f));
- while ((txt= br.readLine())!=null) {
- if(txt.startsWith("/sdcard")){
- list.add(txt);
- }
- }
- ok=false;
- }catch(Exception e){}
- while(ok){
- try
- {
- Thread.sleep(500);
- }
- catch (InterruptedException e)
- {}
- }
- composeFile(list,dst);
- System.out.println("完成合并!");
- }
- public static void composeFile(List<String> tsList, String folderPath) {
- String fileOutPath = folderPath ;
- try {
- try (FileOutputStream fileOutputStream = new FileOutputStream(new File(fileOutPath))){
- byte[] bytes = new byte[1024];
- int length;
- for (String nodePath : tsList) {
- File file = new File(nodePath);
- if (!file.exists()) {
- continue;
- }
- try {
- try (FileInputStream fis = new FileInputStream(file);) {
- try {
- while ((length = fis.read(bytes)) != -1) {
- try {
- fileOutputStream.write(bytes, 0, length);
- } catch (IOException e) {}
- }
- } catch (IOException e) {}
- }
- } catch (FileNotFoundException e) {} catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- } catch (FileNotFoundException e) {} catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
复制代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- char ch,file[100]={0},file1[100]={0};
- printf("输入m3u8文件路径\n");
- scanf("%s",file);
- printf("输入导出文件路径\n");
- scanf("%s",file1);
- FILE* dstfile = fopen(file,"wb+");
- FILE* srcfile = fopen(file1,"r");
- FILE* temp = NULL;
- char name[100];
- while (fscanf(srcfile, "%s", name) != EOF)
- if (strlen(name) == 59||strlen(name) == 60||strlen(name) == 61||strlen(name) == 62)//文件名长度刚好都一样
- {
- temp = fopen(name, "rb");
- char ch = fgetc(temp);
- while (!feof(temp))
- {
- fputc(ch, dstfile);
- ch = fgetc(temp);//最后一次执行此行,文件读不出,ch仍然是上一次的值,之后feof(temp)成立
- }
- }
- return 0;
- }
复制代码 |
-
查看全部评分
总评分:好评 +1
金币 +1
|