随手用Java写的bilibili缓存视频转换器(合成分离的视频和音频)

使用java随手写成,有需要可以自行修改。
项目使用到了fastjson,可以自行替换成其他json解析工具。
写这个的原因是因为下载到的其他工具弄出来的视频标题过长(应该取entry.json中的subtitle,而不是download_subtitle)。
使用前需要在电脑下载ffmpeg,并添加到环境变量中。
在java中执行cmd命令请参考:这里
遍历文件请参考:这里

import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.nio.charset.Charset;

public class MergeBilibliCache {
    static String path = "D:\\b站离线备份\\850463803";
    static String outputPath = "D:\\b站离线备份\\850463803\\output";
    static String baseCommand="ffmpeg -i \"%s\" -i \"%s\" -vcodec copy -acodec copy \"%s\"";//注意路径要加上引号!不然遇到有空格的文件名或者路径教你做人
    public static void main(String[] args) {
        //要遍历的路径
        File file = new File(path);        //获取其file对象
        File output=new File(outputPath);
        if(!output.exists()){
            output.mkdirs();
        }

        try {
            func(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void func(File file) throws IOException {

        File[] fs = file.listFiles();
        for (File f : fs) {
            if (f.isDirectory())    //若是目录,则递归打印该目录下的文件
            {
                System.out.println("当前位于" + f.getAbsolutePath());
                converter(f.getAbsolutePath());
            }
            if (f.isFile())        //若是文件,直接打印
                System.out.println("当前发现" + f);
        }
    }

    public static void converter(String path) throws IOException {
        File file = new File(path + "\\entry.json");
//        FileInputStream inputStream=new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String builder = null;
        builder = bufferedReader.readLine();
        JSONObject jsonObject = JSONObject.parseObject(builder);
        String name=jsonObject.getJSONObject("page_data").getString("part");
        System.out.println(jsonObject.getJSONObject("page_data").get("part"));
        String newCommand=String.format(baseCommand,path+"\\64\\video.m4s",path+"\\64\\audio.m4s",outputPath+"\\"+name+".mp4");
        execCommand(newCommand);


    }
    //利用文件来缓存,不然很容易导致阻塞
    public static void execCommand(String command) {
        BufferedReader br = null;
        try {
            File file = new File("daemonTmp");
            File tmpFile = new File("daemonTmp\\temp.tmp");//新建一个用来存储结果的缓存文件
            if (!file.exists()) {
                file.mkdirs();
            }
            if (!tmpFile.exists()) {
                tmpFile.createNewFile();
            }
            ProcessBuilder pb = new ProcessBuilder().command("cmd.exe", "/c", command).inheritIO();
            pb.redirectErrorStream(true);//这里是把控制台中的红字变成了黑字,用通常的方法其实获取不到,控制台的结果是pb.start()方法内部输出的。
            pb.redirectOutput(tmpFile);//把执行结果输出。
            pb.start().waitFor();//等待语句执行完成,否则可能会读不到结果。
            InputStream in = new FileInputStream(tmpFile);
            br = new BufferedReader(new InputStreamReader(in, Charset.forName("GBK")));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
            br = null;
            tmpFile.delete();//卸磨杀驴。
            System.out.println("执行完成");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
posted @ 2022-01-28 16:01  JessieLin  阅读(732)  评论(0编辑  收藏  举报