FLV视频合并-JAVA代码

由于项目需要,自己编码了视频合并的JAVA代码
把视频
20170815-114600-114659-00-N-0-1.264.flv
和视频1.flv合并后,生成
temp6666.flv
package com.gy.controller.test;
import com.gy.controller.Tags;
import org.aspectj.util.FileUtil;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TwoFlv2One {

    /***
     * 合并FLV视频
     */
    @Test
    public void gets(){
        try {
            together("E:\\other\\media\\tother\\t\\20170815-114600-114659-00-N-0-1.264.flv",
                    "E:\\other\\media\\tother\\t\\1.flv",
                    "E:\\other\\media\\tother\\temp6666.flv");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * 执行合并
     * @param src1
     * @param src2
     * @param tar
     * @throws IOException
     */
    public static void together(String src1,String src2,String tar) throws IOException{
        File f1 = new File(src1);
        File f2 = new File(src2);
        File f3 = new File(tar);
        byte[] len1 = FileUtil.readAsByteArray(f1);
        byte[] len2 = FileUtil.readAsByteArray(f2);

        int lasttime = getTotalTime(len1);//获取第一个视频的所有时间
        System.out.println("lasttime:" + lasttime);
        addtimes(len2,lasttime);//第二个文件要添加时间撮

        //找到len2 15,16,17==第一个tag长度
        byte b14 = len2[14];
        byte b15 = len2[15];
        byte b16 = len2[16];
        double xx = CodeTools.to10(CodeTools.bytes2HexStringxxx(b14))*256*256+CodeTools.to10(CodeTools.bytes2HexStringxxx(b15))*256+CodeTools.to10(CodeTools.bytes2HexStringxxx(b16));
        System.out.println(xx);


       /* change(len1, len2, "width");//以第二个为准,进行变更
        change(len1, len2, "height");
        change(len1, len2, "framerate");
        change(len1, len2, "videodatarate");
        change(len1, len2, "videocodecid");*/
       // change(len1, len2, "encoder");
        add(len1, len2, "duration");//时间相加
        add(len1,len2,"filesize");//文件大小相加
        //直接改
        //len1[4]=5;//音频

        int dev = 13+(int)xx+11+4;
       // int dev = 13;
        int tlen = len1.length + len2.length;
        byte[] len3 = new byte[tlen-dev];
        for(int i=0; i<len1.length;i++){
            len3[i] = len1[i];
        }
        for(int i=dev; i<len2.length;i++){
            len3[i-dev+len1.length] = len2[i];
        }
        CodeTools.toFile(f3, len3);
    }

    public static Object[][] deal(byte[] bsource){

        //总组数
        byte b40=bsource[40];
        byte b41 = bsource[41];
        double gx = CodeTools.to10(CodeTools.bytes2HexStringxxx(b40))*256+CodeTools.to10(CodeTools.bytes2HexStringxxx(b41));
        int nt =42;
        Object[][] rs = new Object[(int)gx+1][];
        for(int j =0;j<gx;j++){
            Object[] obj = new Object[4];
            double dx = CodeTools.to10(CodeTools.bytes2HexStringxxx(bsource[nt]))*256+CodeTools.to10(CodeTools.bytes2HexStringxxx(bsource[nt + 1]));
            nt = nt+2;
            byte[] ts = new byte[(int)dx];
            int k = 0;
            for(int i=0;i<dx;i++){
                ts[k++] = bsource[nt+i];
            }
            nt =nt+k;
            String property = CodeTools.toStr(ts);
            System.out.println(property);
            obj[0] = property;

            String value = null;
            if(property.equals("encoder")){
                obj[2]=nt;
                byte[] tf = new byte[16];
                int kf = 0;
                for(int i=0;i<16;i++){
                    tf[kf++] = bsource[nt+i];
                }
                value = CodeTools.toStr(tf);
                System.out.println(value);
                System.out.println("nt:" + nt);
                nt=nt+16;
                obj[3]=nt;
            }else{
                nt = nt+1;
                obj[2]=nt;
                //值
                byte[] tf = new byte[8];
                int kf = 0;
                for(int i=0;i<8;i++){
                    tf[kf++] = bsource[nt+i];
                }
                nt = nt+8;
                Double v = CodeTools.bytes2Double(CodeTools.reverse(tf));
                System.out.println(v);
                value=v+"";
                System.out.println("nt:" + nt);
                obj[3]=nt;
            }
            obj[1] = value;
            rs[j] = obj;
        }
        rs[(int)gx]=new Object[]{"site","",0,nt};
        return rs;
    }


    @Test
    public void aaa(){
        System.out.println(CodeTools.to10("B8"));
        byte b = -72;
        String aa = CodeTools.bytes2HexStringxxx(b);
        System.out.println(aa);
    }
private static Object[] getObj(byte[] res,String property){
    Object[][] params = deal(res);
    if(params!=null&&params.length>0){
        for(int i=0;i<params.length;i++){
            System.out.println("属性:"+params[i][0]+" 值:"+params[i][1]+" 始:"+params[i][2]+" 末:"+params[i][3]);
            //修改属性
            if(property.equals((String) params[i][0])){
                return params[i];
            }
        }
    }
    return null;
}



    private static void add(byte[] desc,byte[] sorece,String property){
        Object[] descObj = getObj(desc, property);
        Object[] sourceObj = getObj(sorece,property);
        addRes(desc,descObj,sourceObj,property);
    }

    private static  void addRes(byte[] res,Object[] descObj,Object[] sourceObj,String changeProperty){
        if(!(descObj!=null&&descObj.length>0&&sourceObj!=null&&sourceObj.length>0)){
            return;
        }
        for(int i=0;i<sourceObj.length;i++){
            String property = (String)sourceObj[0];
            String value = (String)sourceObj[1];
            if(property.equals(changeProperty)){
                for(int j = 0;j<descObj.length;j++){
                    String property1 = (String)descObj[0];
                    int st1 = Integer.valueOf(descObj[2]+"");
                    int et1 = Integer.valueOf(descObj[3]+"");
                    double value1 = Double.valueOf(descObj[1] + "");
                    if(property.equals(property1)){
                        byte[] bw =  CodeTools.reverse(CodeTools.double2Bytes(Double.valueOf(value).intValue()+value1));
                        int k = 0;
                        for(int f = st1;f<et1;f++){
                            res[f]=bw[k++];
                        }
                    }
                }
            }else{
                continue;
            }
        }
    }


    private static byte[] change(byte[] desc,byte[] source,String property){
        Object[] descObj = getObj(desc,property);
        Object[] sourceObj = getObj(source,property);
        return updateRes(desc,descObj,sourceObj,property);
    }

    private static  byte[] updateRes(byte[] desc,Object[] descObj,Object[] sourceObj,String changeProperty){
        if(!(descObj!=null&&descObj.length>0)){
        }
        if(descObj!=null&&sourceObj!=null)
        for(int i=0;i<sourceObj.length;i++){
            boolean fs = false;
            String property = (String)sourceObj[0];
            String value = (String)sourceObj[1];
            if(property.equals(changeProperty)){
                for(int j = 0;j<descObj.length;j++){
                    String property1 = (String)descObj[0];
                    int st1 = Integer.valueOf(descObj[2]+"");
                    int et1 = Integer.valueOf(descObj[3]+"");
                    if(property.equals(property1)){
                        byte[] bw =  CodeTools.reverse(CodeTools.double2Bytes(Double.valueOf(value)));
                        int k = 0;
                        for(int f = st1;f<et1;f++){
                            desc[f]=bw[k++];
                        }
                        fs = true;
                    }
                }
            }else{
                continue;
            }
        }
        return desc;
    }

    private static Object[] getObj(Object[] sourceObj,String changeProperty){
        for(int i=0;i<sourceObj.length;i++) {
            String property = (String) sourceObj[0];
            if (property.equals(changeProperty)) {
                return sourceObj;
            }
        }
        return null;
    }
    /**获取文件所有时间*/
    public static int getTotalTime(byte[] bytes){
        if(bytes!=null){
            byte[] bw = new byte[4];
            int k = 0;
            int len = bytes.length;
            byte b0 = bytes[len-4];
            byte b1 = bytes[len-3];
            byte b2 = bytes[len-2];
            byte b3 = bytes[len-1];
            int rs = CodeTools.bytesToInt2(new byte[]{b0,b1,b2,b3},0);
            System.out.println("rs:"+rs);
            int s = bytes.length-4-rs+5-1;
            int e = bytes.length-4-rs+8-1;
            byte k0 = bytes[s];
            byte k1 = bytes[s+1];
            byte k2 = bytes[s+2];
            byte k3 = bytes[s+3];
            int result = CodeTools.bytesToInt2(new byte[]{k3,k0,k1,k2},0);
            System.out.println("lasttime:"+result);
            return result;
        }
        return 0;
    }

    /***
     * 时间相加
     * @param bytes
     * @param times
     */
    public static void addtimes(byte[] bytes,int times){
        List<Tags> list = getAllTag(bytes);
        if(list!=null&&list.size()>0){
            int i = 0;
            for(Tags tag :list){
                if(i++>0){
                    int s =tag.getS()+5;
                    s=s-1;
                    byte k0 = bytes[s];
                    byte k1 = bytes[s+1];
                    byte k2 = bytes[s+2];
                    byte k3 = bytes[s+3];
                    int result = CodeTools.bytesToInt2(new byte[]{k3, k0, k1, k2}, 0);
                    int newtimes = result +times;
                    System.out.println("时间==="+newtimes/1000);
                    byte[] rb = CodeTools.intToBytes2(newtimes);
                    bytes[s]=rb[1];
                    bytes[s+1]=rb[2];
                    bytes[s+2]=rb[3];
                    bytes[s+3]=rb[0];
                }
            }
        }
    }

    /***
     * 获取所有tag
     * @param bytes
     * @return
     */
    public static List<Tags> getAllTag(byte[] bytes){//获取所有tag标签
        List<Tags> list = new ArrayList<>();
        int s = 13;
        int e = 0;
        while (e<=bytes.length-10){
            Tags tag = setTags(bytes, s);
            list.add(tag);
            e = tag.getE();
            s = e;
        }
        return list;
    }

    /***
     * 设置值
     * @param bytes
     * @param s
     * @return
     */
    private static Tags setTags(byte[] bytes,int s){
        Tags tags = new Tags();
        byte k0 = bytes[s+1];
        byte k1 = bytes[s+2];
        byte k2 = bytes[s+3];
        int result = CodeTools.bytesToInt2(new byte[]{0,k0,k1,k2},0);
        int presize = result+11;
        tags.setPresize(presize);
        tags.setS(s);
        tags.setE(s+presize+4);
        byte[] b11 = new byte[11];
        int k = 0;
        for(int i=0;i<11;i++){
            b11[k++]=bytes[s+i];
        }
        tags.setTag11(b11);
        return tags;
    }

}
 
 
 
 
package com.gy.controller;

import lombok.Data;

/**
 * Created by Administrator on 2017/8/18 0018.
 */
@Data
public class Tags {
    private byte[] tag11;
    private int presize;
    private int s;
    private int e;
}

 
 
//转换工具网上抄了下,大同小异。
package com.gy.controller.test;

import org.junit.Test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class CodeTools {

    public static int to10(String b){
        int ii = Integer.parseInt(b,16);
        return ii;
    }

    public static String bytes2HexStringxxx(byte bx) {
        String ret = "";
        String hex = Integer.toHexString(bx & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        ret += hex.toUpperCase();
        return ret;
    }


    public static byte[] double2Bytes(double d) {
        long value = Double.doubleToRawLongBits(d);
        byte[] byteRet = new byte[8];
        for (int i = 0; i < 8; i++) {
            byteRet[i] = (byte) ((value >> 8 * i) & 0xff);
        }
        return byteRet;
    }

    public static double bytes2Double(byte[] arr) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (arr[i] & 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }

    public static byte[] reverse(byte[] b){
        byte[] r = new byte[b.length];
        int k = 0;
        for(int i=b.length-1;i>=0;i--){
            r[k++]=b[i];
        }
        return r;
    }

    public static String toStr(byte[] b){
        return new String(b);
    }

    public static File toFile(File file, byte[] b){
        try{
            BufferedOutputStream bos = null;
            FileOutputStream fos = null;
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(b);
        }catch (Exception e){

        }
        return file;
    }
    public static byte[] toByte(String s){
        return s.getBytes();
    }


    public static int bytesToInt2(byte[] src, int offset) {
        int value;
        value = (int) ( ((src[offset] & 0xFF)<<24)
                |((src[offset+1] & 0xFF)<<16)
                |((src[offset+2] & 0xFF)<<8)
                |(src[offset+3] & 0xFF));
        return value;
    }
    public static byte[] intToBytes2(int value)
    {
        byte[] src = new byte[4];
        src[0] = (byte) ((value>>24) & 0xFF);
        src[1] = (byte) ((value>>16)& 0xFF);
        src[2] = (byte) ((value>>8)&0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }
    @Test
    public  void aaa(){
        int ii = bytesToInt2(hexStringToByteArray("00002800"), 0);
        System.out.println("ii:"+ii);
    }


    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                    .digit(s.charAt(i + 1), 16));
        }
        return b;
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

英雄汉孑

每一滴心意都是对知识分享的赞美

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值