文件合块时的异常(mp4文件)

本文介绍了如何解决在合并分块视频时,由于文件名顺序导致播放失败的问题。作者提供了两种解决方案:一是使用Arrays工具对文件进行排序,二是重新定义for循环中的文件引用,确保合块操作按照正确的顺序进行。

文件上传时进行合块时,代码运行正常,也合出了一个符合预期的文件,但是点击却无法播放

//测试文件合块方法
    @Test
    public void mergeChunk() throws Exception {
        //指定块文件所在目录
        String chunkFileDirPath = "D:\\ffmpeg-vedio\\chunk\\";
        File chunkDirFile = new File(chunkFileDirPath);
        //获取块文件列表信息
        File[] files = chunkDirFile.listFiles();
        //指定合并文件所在路径
        String mergeFilePath = "D:\\ffmpeg-vedio\\bys.mp4";
        File mergeFile = new File(mergeFilePath);
        //创建输出流
        RandomAccessFile rw = new RandomAccessFile(mergeFile, "rw");
        rw.seek(0);//强制从参数为止开始写
        //创建缓冲区
        byte[] bytes = new byte[1024];
        int len = -1;
        //遍历块文件,写入输出流
        for (File file : files) {
            //创建输入流
            RandomAccessFile r = new RandomAccessFile(file, "r");
            while ((len = r.read(bytes)) != -1) {
                rw.write(bytes, 0, len);
            }
            r.close();
        }
        rw.close();
    }

 原因:分块时文件名为数字,合块时顺序为0,1,10(超过10块)

        合成顺序与分块顺序不同

解决方法:

1.通过Arrays工具类转化为list集合,先进行排序后再合块

//测试文件合块方法
    @Test
    public void mergeChunk1() throws Exception {
        //指定块文件所在目录
        String chunkFileDirPath = "D:\\ffmpeg-vedio\\chunk\\";
        File chunkDirFile = new File(chunkFileDirPath);
        //获取块文件列表信息
        File[] files = chunkDirFile.listFiles();

        List<File> fileList = Arrays.asList(files);
        fileList.sort(new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if(Integer.parseInt(o1.getName())>Integer.parseInt(o2.getName())){
                    return 1;
                }else{
                    return -1;
                }
            }
        });
        //指定合并文件所在路径
        String mergeFilePath = "D:\\ffmpeg-vedio\\bys.mp4";
        File mergeFile = new File(mergeFilePath);
        //创建输出流
        RandomAccessFile rw = new RandomAccessFile(mergeFile, "rw");
        rw.seek(0);//强制从参数为止开始写
        //创建缓冲区
        byte[] bytes = new byte[1024];
        int len = -1;
        //遍历块文件,写入输出流
        for (File file : fileList) {
            //创建输入流
            RandomAccessFile r = new RandomAccessFile(file, "r");
            while ((len = r.read(bytes)) != -1) {
                rw.write(bytes, 0, len);
            }
            r.close();
        }
        rw.close();
    }

 2.for循环中自己重新定义一个file

@Test
    public void mergeChunk2() throws Exception {
        //指定块文件所在目录
        String chunkFileDirPath = "D:\\ffmpeg-vedio\\chunk\\";
        File chunkDirFile = new File(chunkFileDirPath);
        //获取块文件列表信息
        File[] files = chunkDirFile.listFiles();
        //指定合并文件所在路径
        String mergeFilePath = "D:\\ffmpeg-vedio\\bys.mp4";
        File mergeFile = new File(mergeFilePath);
        //创建输出流
        RandomAccessFile rw = new RandomAccessFile(mergeFile, "rw");
        rw.seek(0);//强制从参数为止开始写
        //创建缓冲区
        byte[] bytes = new byte[1024];
        int len = -1;
        //遍历块文件,写入输出流
        for (int i = 0; i < files.length; i++) {
            File file = new File(chunkFileDirPath + i);
            //创建输入流
            RandomAccessFile r = new RandomAccessFile(file, "r");
            while ((len = r.read(bytes)) != -1) {
                rw.write(bytes, 0, len);
            }
            r.close();
        }
        rw.close();
    }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值