java视频拆分多个文件保存与多个文件合并成一个视频

今天闲着没事写一个小的测试,就是写了一个视频的拆分,我是把一个文件拆分为多个txt文件保存,然后再把多个txt文件合并为一个视频并播放(这也是断点续传的一部分吧)

一个文件拆分为多个文件的代码的测试:

@Test
    public void test1()  {
        File oldfile = new File("D:/视频/"+"1.mp4");
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
            try {
                fileInputStream = new FileInputStream(oldfile);
                int num=1;
                byte[] aByte = new byte[153310];
                int length=0;
                while ((length=fileInputStream.read(aByte)) != -1 ) {
                    fileOutputStream = new FileOutputStream("D:/视频/"+num+".txt");
                    fileOutputStream.write(aByte,0,length);
                    num++;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

这面这个的话我是一次153310字节保存到一个txt文件的,这样写肯定是不太好,但是我本来想用读取1024然后一点点追加到文件,奈何我没想到实现,这种应该可以用在while循环里面然后取出文件,用文件大小比较字节,然后不够字节的话进行追加,大伙可以想一想怎么实现的的.

 上面的1.mp4是原始文件,txt文件就是拆分之后出现的文件

然后对文件进行合并

@Test
    public void test2()  {
        File newfile = new File("D:/视频/"+"2.mp4");
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        for (int i = 1; i <=10; i++) {
            File subfile= new File("D:/视频/"+i+".txt");
            try {
                fileInputStream=new FileInputStream(subfile);
                fileOutputStream = new FileOutputStream(newfile,true);
                byte[] aByte = new byte[1024];
                int length=0;
                while ((length=fileInputStream.read(aByte)) != -1 ) {
                    fileOutputStream.write(aByte,0,length);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

也是可以播放的 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值