package com.mortals.xhx.tts.utils;

import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.List;

/**
 * @author ZYW
 * @date 2024-07-22 16:19
 */

public class SpliceMp3Util {

    /**
     * 返回分离出MP3文件中的数据帧的文件路径
     *
     */
    public static String fenLiData(String path,String lujing) throws IOException {
        ClassLoader classLoader = SpliceMp3Util.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream(path);
        String fileName = path.substring(path.lastIndexOf("/")+1);

        File file = new File(lujing+fileName);
        File file1 = new File(lujing+fileName + "01");// 分离ID3V2后的文件,这是个中间文件，最后要被删除
        File file2 = new File(lujing+fileName + "001");// 分离id3v1后的文件
        RandomAccessFile rf = new RandomAccessFile(file, "rw");// 随即读取文件
        copyInputStreamToRandomAccessFile(inputStream,rf);
        FileOutputStream fos = new FileOutputStream(file1);
        byte ID3[] = new byte[3];
        rf.read(ID3);
        String ID3str = new String(ID3);
        // 分离ID3v2
        if (ID3str.equals("ID3")) {
            rf.seek(6);
            byte[] ID3size = new byte[4];
            rf.read(ID3size);
            int size1 = (ID3size[0] & 0x7f) << 21;
            int size2 = (ID3size[1] & 0x7f) << 14;
            int size3 = (ID3size[2] & 0x7f) << 7;
            int size4 = (ID3size[3] & 0x7f);
            int size = size1 + size2 + size3 + size4 + 10;
            rf.seek(size);
            int lens = 0;
            byte[] bs = new byte[1024*4];
            while ((lens = rf.read(bs)) != -1) {
                fos.write(bs, 0, lens);
            }
            fos.close();
            rf.close();
        } else {// 否则完全复制文件
            int lens = 0;
            rf.seek(0);
            byte[] bs = new byte[1024*4];
            while ((lens = rf.read(bs)) != -1) {
                fos.write(bs, 0, lens);
            }
            fos.close();
            rf.close();
        }
        RandomAccessFile raf = new RandomAccessFile(file1, "rw");
        byte TAG[] = new byte[3];
        raf.seek(raf.length() - 128);
        raf.read(TAG);
        String tagstr = new String(TAG);
        if (tagstr.equals("TAG")) {
            FileOutputStream fs = new FileOutputStream(file2);
            raf.seek(0);
            byte[] bs=new byte[(int)(raf.length()-128)];
            raf.read(bs);
            fs.write(bs);
            raf.close();
            fs.close();
        } else {// 否则完全复制内容至file2
            FileOutputStream fs = new FileOutputStream(file2);
            raf.seek(0);
            byte[] bs = new byte[1024*4];
            int len = 0;
            while ((len = raf.read(bs)) != -1) {
                fs.write(bs, 0, len);
            }
            raf.close();
            fs.close();
        }
        if (file1.exists())// 删除中间文件
        {
            file1.delete();

        }
        if (file.exists())// 删除中间文件
        {
            file.delete();

        }
        return file2.getAbsolutePath();
    }


    public static void copyInputStreamToRandomAccessFile(InputStream inputStream,RandomAccessFile randomAccessFile) throws IOException {
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            randomAccessFile.write(buffer, 0, bytesRead);
        }
    }

    /**
     * 返回合并后的文件的路径名,默认放在第一个文件的目录下
     * @return
     * @throws IOException
     */
    public static String heBingMp3(List<String> pathList) throws IOException {
        String luJing = "/tmp/";
        if(System.getProperty("os.name").startsWith("Windows")){  //windows电脑测试路径
            luJing= "F:\\\\";
        }
        File f=new File(luJing);
        f.mkdirs();
        //生成处理后的文件
        File file2=new File(luJing+"new.mp3");
        if(file2.exists()){
            file2.delete();
        }
        FileOutputStream out;
        for (int i = 0; i < pathList.size(); i++) {
            String fenLiData = fenLiData(pathList.get(i),luJing);
            File file=new File(fenLiData);
            FileInputStream in=new FileInputStream(file);
            if(i == 0){
                out=new FileOutputStream(file2);
            }else {
                out=new FileOutputStream(file2,true);//在文件尾打开输出流
            }
            byte bs[]=new byte[1024*4];
            int len=0;
            //先读第一个
            while((len=in.read(bs))!=-1){
                out.write(bs,0,len);
            }
            in.close();
            out.close();
            if(file.exists())file.delete();
        }
        return file2.getAbsolutePath();
    }
}
