IO流拆分和合并视频功能

  1. 假设有一个大小为10G的视频文件(文件大小和类型不限定),然后将该文件分割成几个小一些的片段,并分别保存成一个个小的文件。
  2. . 把上面被分割的若干小文件,合并起来,恢复成原来的文件。 编写一个工具程序,实现上述功能(合并时注意小文件的顺序)
package com.Work4;

import org.junit.Test;
import java.io.*;

/**
 * @Author: 廾匸
 * @Date: 2020/11/29 14:29
 * @Description: 
 * @version: 1.01
 */
public class Four {

    @Test
    public void test01(){
        reads(new File("E:\\WeChat.mp4"),"G:\\WeChat");
        try {
            for (int i = 0; i < 5; i++) {
                merge(new File("G:\\WeChat" +"\\" + "文件"+ i + ".mp4"),
                        new File("G:\\合并后视频.mp4"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 拆分
    public void reads(File file,String name){
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(file);
            byte [] bytes = new byte[1024*1024];
            int len;
            for (int i = 0; i < 5; i++) {
                output = new FileOutputStream(new File(name,"文件" + i + ".mp4"));
                int temp = 0;  // 所截取视频的大小
                while((len = input.read(bytes)) != -1){
                    output.write(bytes,0,len);
                    temp++;
                    output.write(bytes,0,len);
                    if(i != 4 && temp >= 2){
                        break;
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                output.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 合并
    /**
     * 
     * @param file  要合并的视频文件
     * @param newFile  // 合并到的视频文件
     * @throws IOException
     */
    public void merge(File file,File newFile) throws IOException {
        InputStream input = null;
        OutputStream output = null;
        input = new FileInputStream(file);
        byte [] bytes = new byte[1024*1024];
        output = new FileOutputStream(newFile,true);
        int len;
        while((len = input.read(bytes)) != -1){
            output.write(bytes,0,len);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值