原创

    哔哩哔哩提取视频代码【超实用!】

    01-SearchAndTransferVideo

    package club.guoshizhan.Bilibili;
    
    import java.io.*;
    
    /**
     * @author: guoshizhan
     * @create: 2021-03-21 16:19
     * @description: 查找指定格式的视频文件,并传输到指定目录
     */
    public class SearchAndTransferVideo {
    
        // 定义 count,用于下列指定格式打印输出
        private static int count = 0;
        private final static String extension = ".mp4";
    
        public static void main(String[] args) {
            File source = new File("D:\\Downloads\\04-哔哩哔哩下载\\95396959");
            File desc = new File("D:\\Downloads\\WEB");
            searchAndTransferVideo(source, desc);
        }
    
        public static void searchAndTransferVideo(File source, File desc) {
            File[] files = source.listFiles();
    
            if (files != null) {
                for (File str : files) {
                    if (str.isDirectory()) {
                        searchAndTransferVideo(str, desc);
                    }
                    if (str.isFile() && str.getName().endsWith(extension)) {
                        String[] strings = str.getName().split("_");
                        int temp = Integer.parseInt(strings[1]);
    
                        if (temp < 10) {
                            strings[1] = "0" + strings[1];
                        }
                        /*if (temp < 100 && temp >= 10) {
                            strings[1] = "0" + strings[1];
                        }*/
    
                        String finalName = strings[1] + extension;
                        System.out.print((++count) + "\t" + finalName + "\t");
                        String dir = desc + "\\" + finalName;    // 把 .mp4 文件传输到 dir 这个目录
    
                        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str));
                             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dir))
                        ) {
                            // 读写数据
                            int len;
                            byte[] bytes = new byte[16 * 1024];
                            while ((len = bis.read(bytes)) != -1) {
                                bos.write(bytes, 0, len);
                            }
                            System.out.println("<========== 传输完成!=========>");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    
    

    02-GetFileNameOfVideo

    1、VideoInfo

    package club.guoshizhan.Bilibili.domain;
    
    /**
     * @author: guoshizhan
     * @create: 2021-03-21 17:46
     * @description: B站 json 文件的视频信息实体类
     */
    public class VideoInfo {
    
        private int MediaType;
        private int CodecId;
        private String CodecName;
        private int VideoWidth;
        private int VideoHeight;
        private long Bandwidth;
        private double FrameRate;
    
        public void setMediaType(int MediaType) {
            this.MediaType = MediaType;
        }
    
        public int getMediaType() {
            return MediaType;
        }
    
        public void setCodecId(int CodecId) {
            this.CodecId = CodecId;
        }
    
        public int getCodecId() {
            return CodecId;
        }
    
        public void setCodecName(String CodecName) {
            this.CodecName = CodecName;
        }
    
        public String getCodecName() {
            return CodecName;
        }
    
        public void setVideoWidth(int VideoWidth) {
            this.VideoWidth = VideoWidth;
        }
    
        public int getVideoWidth() {
            return VideoWidth;
        }
    
        public void setVideoHeight(int VideoHeight) {
            this.VideoHeight = VideoHeight;
        }
    
        public int getVideoHeight() {
            return VideoHeight;
        }
    
        public void setBandwidth(long Bandwidth) {
            this.Bandwidth = Bandwidth;
        }
    
        public long getBandwidth() {
            return Bandwidth;
        }
    
        public void setFrameRate(double FrameRate) {
            this.FrameRate = FrameRate;
        }
    
        public double getFrameRate() {
            return FrameRate;
        }
    
    }
    

    2、AudioInfo

    package club.guoshizhan.Bilibili.domain;
    
    /**
     * @author: guoshizhan
     * @create: 2021-03-21 17:45
     * @description: B站 json 文件的音频信息实体类
     */
    public class AudioInfo {
    
        private int MediaType;
        private int CodecId;
        private String CodecName;
        private int VideoWidth;
        private int VideoHeight;
        private long Bandwidth;
        private int FrameRate;
    
        public void setMediaType(int MediaType) {
            this.MediaType = MediaType;
        }
    
        public int getMediaType() {
            return MediaType;
        }
    
        public void setCodecId(int CodecId) {
            this.CodecId = CodecId;
        }
    
        public int getCodecId() {
            return CodecId;
        }
    
        public void setCodecName(String CodecName) {
            this.CodecName = CodecName;
        }
    
        public String getCodecName() {
            return CodecName;
        }
    
        public void setVideoWidth(int VideoWidth) {
            this.VideoWidth = VideoWidth;
        }
    
        public int getVideoWidth() {
            return VideoWidth;
        }
    
        public void setVideoHeight(int VideoHeight) {
            this.VideoHeight = VideoHeight;
        }
    
        public int getVideoHeight() {
            return VideoHeight;
        }
    
        public void setBandwidth(long Bandwidth) {
            this.Bandwidth = Bandwidth;
        }
    
        public long getBandwidth() {
            return Bandwidth;
        }
    
        public void setFrameRate(int FrameRate) {
            this.FrameRate = FrameRate;
        }
    
        public int getFrameRate() {
            return FrameRate;
        }
    
    }
    

    3、JsonBilibiliBean

    package club.guoshizhan.Bilibili.domain;
    
    import java.util.Date;
    import java.util.List;
    
    /**
     * @author: guoshizhan
     * @create: 2021-03-21 17:48
     * @description: B站的 json 文件实体类
     */
    public class JsonBilibiliBean {
    
        private int Type;
        private String Aid;
        private String Bid;
        private String Cid;
        private String SeasonId;
        private String EpisodeId;
        private String Title;
        private String Uploader;
        private String Description;
        private String CoverURL;
        private String Tag;
        private String From;
        private String PartNo;
        private String PartName;
        private int Format;
        private int TotalParts;
        private long DownloadTimeRelative;
        private Date CreateDate;
        private String TotalTime;
        private List<Long> PartTime;
        private long TotalSizeByte;
        private boolean IsSinglePart;
        private boolean IsDash;
        private boolean IsMerged;
        private VideoInfo VideoInfo;
        private List<AudioInfo> AudioInfo;
    
        public void setType(int Type) {
            this.Type = Type;
        }
    
        public int getType() {
            return Type;
        }
    
        public void setAid(String Aid) {
            this.Aid = Aid;
        }
    
        public String getAid() {
            return Aid;
        }
    
        public void setBid(String Bid) {
            this.Bid = Bid;
        }
    
        public String getBid() {
            return Bid;
        }
    
        public void setCid(String Cid) {
            this.Cid = Cid;
        }
    
        public String getCid() {
            return Cid;
        }
    
        public void setSeasonId(String SeasonId) {
            this.SeasonId = SeasonId;
        }
    
        public String getSeasonId() {
            return SeasonId;
        }
    
        public void setEpisodeId(String EpisodeId) {
            this.EpisodeId = EpisodeId;
        }
    
        public String getEpisodeId() {
            return EpisodeId;
        }
    
        public void setTitle(String Title) {
            this.Title = Title;
        }
    
        public String getTitle() {
            return Title;
        }
    
        public void setUploader(String Uploader) {
            this.Uploader = Uploader;
        }
    
        public String getUploader() {
            return Uploader;
        }
    
        public void setDescription(String Description) {
            this.Description = Description;
        }
    
        public String getDescription() {
            return Description;
        }
    
        public void setCoverURL(String CoverURL) {
            this.CoverURL = CoverURL;
        }
    
        public String getCoverURL() {
            return CoverURL;
        }
    
        public void setTag(String Tag) {
            this.Tag = Tag;
        }
    
        public String getTag() {
            return Tag;
        }
    
        public void setFrom(String From) {
            this.From = From;
        }
    
        public String getFrom() {
            return From;
        }
    
        public void setPartNo(String PartNo) {
            this.PartNo = PartNo;
        }
    
        public String getPartNo() {
            return PartNo;
        }
    
        public void setPartName(String PartName) {
            this.PartName = PartName;
        }
    
        public String getPartName() {
            return PartName;
        }
    
        public void setFormat(int Format) {
            this.Format = Format;
        }
    
        public int getFormat() {
            return Format;
        }
    
        public void setTotalParts(int TotalParts) {
            this.TotalParts = TotalParts;
        }
    
        public int getTotalParts() {
            return TotalParts;
        }
    
        public void setDownloadTimeRelative(long DownloadTimeRelative) {
            this.DownloadTimeRelative = DownloadTimeRelative;
        }
    
        public long getDownloadTimeRelative() {
            return DownloadTimeRelative;
        }
    
        public void setCreateDate(Date CreateDate) {
            this.CreateDate = CreateDate;
        }
    
        public Date getCreateDate() {
            return CreateDate;
        }
    
        public void setTotalTime(String TotalTime) {
            this.TotalTime = TotalTime;
        }
    
        public String getTotalTime() {
            return TotalTime;
        }
    
        public void setPartTime(List<Long> PartTime) {
            this.PartTime = PartTime;
        }
    
        public List<Long> getPartTime() {
            return PartTime;
        }
    
        public void setTotalSizeByte(long TotalSizeByte) {
            this.TotalSizeByte = TotalSizeByte;
        }
    
        public long getTotalSizeByte() {
            return TotalSizeByte;
        }
    
        public void setIsSinglePart(boolean IsSinglePart) {
            this.IsSinglePart = IsSinglePart;
        }
    
        public boolean getIsSinglePart() {
            return IsSinglePart;
        }
    
        public void setIsDash(boolean IsDash) {
            this.IsDash = IsDash;
        }
    
        public boolean getIsDash() {
            return IsDash;
        }
    
        public void setIsMerged(boolean IsMerged) {
            this.IsMerged = IsMerged;
        }
    
        public boolean getIsMerged() {
            return IsMerged;
        }
    
        public void setVideoInfo(VideoInfo VideoInfo) {
            this.VideoInfo = VideoInfo;
        }
    
        public VideoInfo getVideoInfo() {
            return VideoInfo;
        }
    
        public void setAudioInfo(List<AudioInfo> AudioInfo) {
            this.AudioInfo = AudioInfo;
        }
    
        public List<AudioInfo> getAudioInfo() {
            return AudioInfo;
        }
    
    }
    

    4、GetFileNameOfVideo

    package club.guoshizhan.Bilibili;
    
    import club.guoshizhan.Bilibili.domain.JsonBilibiliBean;
    import com.alibaba.fastjson.JSON;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author: guoshizhan
     * @create: 2021-03-21 17:20
     * @description: 获取视频的名字
     */
    public class GetFileNameOfVideo {
    
        private final static List<String> list = new ArrayList<>();
    
        public static void main(String[] args) throws Exception {
            File source = new File("D:\\Downloads\\04-哔哩哔哩下载\\95396959\\");
            List<String> lists = getFileNameOfVideo(source);
            lists.forEach(System.out::println);
        }
    
        public static List<String> getFileNameOfVideo(File file) throws Exception {
            File[] files = file.listFiles();
    
            if (files == null) {
                return new ArrayList<>();
            }
    
            for (File str : files) {
                // 2-如果是目录,进行递归遍历
                if (str.isDirectory()) {
                    getFileNameOfVideo(str);
                }
    
                // 如果是文件且以 info 结尾,那么目标找到,提取它里面我们需要的内容
                if (str.isFile() && str.getName().endsWith(".info")) {
                    // 3-创建输入流,读取内容
                    File fileContent = new File(file + "\\" + str.getName());
                    FileInputStream fileInputStream = new FileInputStream(fileContent);
                    // 4-将读取的内容进行提取,得到我们需要的
                    byte[] bytes = new byte[8 * 1024];
                    // 5-循环遍历读取内容
                    int len;
                    while ((len = fileInputStream.read(bytes)) != -1) {
                        // 6-以逗号切割内容
    
                        String res = new String(bytes); // 返回 json 格式的数据
    //                    System.out.println(res);
                        JsonBilibiliBean jsonBilibiliBean = JSON.parseObject(res, JsonBilibiliBean.class);
                        String partNo = jsonBilibiliBean.getPartNo();
    
                        if (Integer.parseInt(partNo) < 10) {
                            partNo = "0" + partNo;
                        }
                        String[] split = jsonBilibiliBean.getPartName().split("\\.");
                        String partName = partNo + "-" + split[1];
                        list.add(partName);
                    }
                    fileInputStream.close();
                }
            }
    
            return list;
        }
    }
    

    03-RenameVideo

    package club.guoshizhan.Bilibili.domain;
    
    import club.guoshizhan.Bilibili.GetFileNameOfVideo;
    
    import java.io.File;
    import java.util.List;
    
    /**
     * @author: guoshizhan
     * @create: 2021-03-21 18:44
     * @description:
     */
    public class RenameVideo {
        public static void main(String[] args) throws Exception {
            String path = "D:\\Downloads\\00-学习视频资料\\RabbitMQ教程-B站-编程不良人\\视频";
            File source = new File("D:\\Downloads\\04-哔哩哔哩下载\\95396959\\");
            File file = new File(path);
            List<String> lists = GetFileNameOfVideo.getFileNameOfVideo(source);
            //lists.forEach(System.out::println);
            renameVideo(file, lists, path);
        }
    
        // 03-给文件进行重命名操作
        public static void renameVideo(File file, List<String> list, String desc) {
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }
            for (File file1 : files) {
                if (!file1.isDirectory()) {
                    String substring = file1.getName();
                    for (String s : list) {
                        // 判断前两个数字是否相等
                        if (substring.substring(0, 2).equals(s.substring(0, 2))) {
                            file1.renameTo(new File(desc + "\\" + s + ".mp4"));
                        }
                    }
                }
            }
        }
    }
    

    04-过期的其他代码

    package club.guoshizhan.Bilibili;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @Author: guoshizhan
     * @Create: 2020/5/1 16:47
     * @Description: 对电脑哔哩哔哩动画下载的视频进行文件复制、批量改名、批量操作等等最终版本
     */
    public class BilibiliTransfer {
    
        // 定义 count,用于下列指定格式打印输出
        private static int count = 0;
        private static List<String> newList = new ArrayList<>();
    
        // 01-在 file 目录中查找含有 flv 结尾的视频并输出
        public static void searchFile(File file) {
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }
    
            for (File str : files) {
                if (str.isDirectory()) {
                    searchFile(str);    // 递归调用
                }
                if (str.isFile() && str.getName().endsWith("flv")) {
                    String[] strings = str.getName().split("_");
                    int temp = Integer.parseInt(strings[1]);
    
                    if (temp < 10) {
                        strings[1] = "00" + strings[1];
                    }
                    if (temp < 100 && temp >= 10) {
                        strings[1] = "0" + strings[1];
                    }
    
                    String finalName = strings[1] + ".flv";
                    System.out.print((++count) + "\t" + finalName + "\t\t");
                    if (count % 10 == 0) {
                        System.out.println();
                    }
    
                    /*try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str));
                         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\WEB\\" + finalName))
                    ) {
                        // 读写数据
                        int len;
                        byte[] bytes = new byte[16 * 1024];
                        while ((len = bis.read(bytes)) != -1) {
                            bos.write(bytes, 0, len);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/
                }
            }
        }
    
        // 02-查找以 info 结尾的文件,解析里面文件,获得每一集视频的标题
        public static List<String> getFileName(File file, List<String> list) throws Exception {
            // 1-遍历目录中的所有文件及文件夹
            File[] files = file.listFiles();
            if (files == null) {
                return null;
            }
            for (File str : files) {
                // 2-如果是目录,进行递归遍历;
                if (str.isDirectory()) {
                    getFileName(str, list);
                }
                if (str.isFile() && str.getName().endsWith("info")) { // 如果是文件且以 info 结尾,那么目标找到,提取它里面我们需要的内容
                    // 3-创建输入流,读取内容
                    File fileContent = new File(file + "\\" + str.getName());
                    FileInputStream fileInputStream = new FileInputStream(fileContent);
                    // 4-将读取的内容进行提取,得到我们需要的
                    byte[] bytes = new byte[8 * 1024];
                    // 5-循环遍历读取内容
                    int len;
                    while ((len = fileInputStream.read(bytes)) != -1) {
                        // 6-以逗号切割内容
                        String strings = new String(bytes);
                        String[] splitArr = strings.split(",");
                        // 7-分析已切割内容
                        String[] split = splitArr[12].split(":");
                        String temp = split[1].substring(1, split[1].length() - 1);
                        if (Integer.parseInt(temp) < 10) {
                            temp = "0" + temp;
                        }
                        /*if (Integer.parseInt(temp) < 100 && Integer.parseInt(temp) >= 10) {
                            temp = "0" + temp;
                        }*/
    
                        String[] split1 = splitArr[13].split(":");
                        String substring = split1[1].substring(4, split1[1].length() - 1);
                        String res = temp + "-" + substring.trim();
                        list.add(res);
                    }
                    fileInputStream.close();
                }
            }
            return list;
        }
    
        // 03-给文件进行重命名操作
        public static void modify(File file, List<String> list) {
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }
            for (File file1 : files) {
                if (!file1.isDirectory()) {
                    String[] substring = file1.getName().split("\\.");
                    for (String s : list) {
                        // 判断前两个数字是否相等
                        if (substring[0].equals(s.substring(0, 3))) {
                            file1.renameTo(new File("E:\\WEB\\" + s + ".flv"));
                        }
                    }
                }
            }
        }
    
    }
    
    
    
    Bilibili
    • 文章作者:GuoShiZhan
    • 创建时间:2021-08-02 21:40:35
    • 更新时间:2021-08-02 21:41:57
    • 版权声明:本文为博主原创文章,未经博主允许不得转载!
    请 在 评 论 区 留 言 哦 ~~~
    1024