起因
昨天晚上從B站電腦客戶端下了一個分集視頻
但是下載后的視頻是這樣的:
視頻名是這樣的:
這樣既不直觀又不美觀,就算把視頻文件放到一個文件夾內,連續看視頻時也不容易記住看到哪個。所以就有了今天的事情。
經過
起初,我的想法是復制出來一個一個該文件名,但是當時我想到,作為一個優秀的程序員,怎么能干這種無腦的活呢?😆(其實是太懶了。。。)
於是,,,就去百度了,百度確實找到一個,但是上面的方法根本沒有寫全,雖然我已經盡力補全,但是他還是漏了一個非常關鍵的一步,所以我就決定自己來寫。
分析
根據下載的文件來看,在info文件中,可以找到原視頻的所有介紹信息,其中當然就有原視頻的文件名(PartName),所以我們只需要把info文件中的文件名提取出來,然后再對flv視頻文件重命名不就完事了嗎。
具體可以分為以下步驟:
-
遍歷我們下載好的視頻目錄,得到info文件集合和flv視頻文件集合;
-
遍歷讀取info文件,從中提取我們需要的視頻文件名;
-
循環遍歷flv文件,提取視頻序號,用來組裝我們想要的視頻名;
-
遍歷flv,給每一個flv視頻文件換上新名字;
代碼實現
-
遍歷視頻目錄,將info文件存入list集合;
1 /** 2 * 遍歷下載目錄,將info文件存入list集合 3 * - 目的:提供info文件給getPartNameList()方法,獲得想要的視頻文件名 4 * 5 * @param downloadPath 6 * @return 7 */ 8 public static List<File> getInfoList(String downloadPath) { 9 File dir = new File(downloadPath); 10 11 // 把下載目錄下的所有文件(可能是目錄也可能是文件)放到數組中 12 File[] subDirOrFile = dir.listFiles(); 13 14 if (subDirOrFile != null) { 15 for (int i = 0; i < subDirOrFile.length; i++) { 16 String fileName = subDirOrFile[i].getName(); 17 // 判斷是否是目錄,如果是目錄繼續遍歷 18 if (subDirOrFile[i].isDirectory()) { 19 getInfoList(subDirOrFile[i].getAbsolutePath()); 20 // 判斷是否以info結尾 21 } else if (fileName.endsWith("info")) { 22 infoList.add(subDirOrFile[i]); 23 } else { 24 continue; 25 } 26 } 27 } 28 return infoList; 29 }
-
遍歷視頻目錄,把flv視頻文件放入list集合;
1 /** 2 * 根據下載路徑,遍歷獲取所有視頻文件list集合 3 * -目的:改名時需要知道原始文件對象 4 * 5 * @param downloadPath 6 * @return 7 */ 8 public static List<File> getFlvList(String downloadPath) { 9 File dir = new File(downloadPath); 10 11 // 把下載目錄下的所有文件放到數組中 12 File[] subDirOrFile = dir.listFiles(); 13 14 if (subDirOrFile != null) { 15 for (int i = 0; i < subDirOrFile.length; i++) { 16 String fileName = subDirOrFile[i].getName(); 17 if (subDirOrFile[i].isDirectory()) { 18 getFlvList(subDirOrFile[i].getAbsolutePath()); 19 } else if (fileName.endsWith("flv")) { 20 flvList.add(subDirOrFile[i]); 21 } else { 22 continue; 23 } 24 } 25 } 26 return flvList; 27 }
-
遍歷讀取info文件,提取需要的視頻名集合;
/** * 讀取 info 文件,獲取視頻文件名 * * @param infoFile * @return */ public static String getPartName(File infoFile) { BufferedReader br = null; String partName = null; try { br = new BufferedReader(new FileReader(infoFile)); String str; while (null != (str = br.readLine())) { // 獲取partName字段對應的文件名 partName = str.split(",")[17].split(":")[1].replace("\"", ""); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } return partName; } /** * 遍歷info 文件list集合,獲得視頻文件名list集合 * * @param infoList * @return */ public static List<String> getPartNameList(List<File> infoList) { List<String> partNameList = new ArrayList<>(); for (int i = 0; i < infoList.size(); i++) { // 調用獲取視頻名的方法 String partName = getPartName(infoList.get(i)); partNameList.add(partName); } return partNameList; }
-
遍歷flv文件集合,組裝文件名;
1 /** 2 * 根據視頻名,flv文件對象,av號來組裝我們想要的文件對象 3 * -用途:重命名的目標文件對象 4 * 5 * @param partName 6 * @param flvFile 7 * @param avNum 8 * @return 9 */ 10 public static File getDestFile(String partName, File flvFile, String avNum) { 11 // 根據flv文件名截取視頻的序號 12 String index = flvFile.getName().split("_")[1]; 13 14 // 截取flv文件路徑,作為重命名文件的路徑 E:\Videos\75233634\ 15 String newPathTemp = flvFile.getPath().split(avNum + "_")[0]; 16 // 判斷該路徑最后有沒有"\" ,沒有則加上 E:\Videos\75233634\ 17 String newPath = newPathTemp.endsWith("\\") ? newPathTemp : newPathTemp + "\\"; 18 // 新的文件路徑:即 E:\Videos\75233634\1_1、這階段該如何學習.flv 19 String newFilePath = newPath + index + "_" + partName + ".flv"; 20 21 File destFile = new File(newFilePath); 22 return destFile; 23 }
-
main方法,完成批量重命名;
1 // 這兩個屬於類的私有屬性,定義在方法外邊,分別代表info文件集合和flv視頻文件集合 2 private static List<File> infoList = new ArrayList<>(); 3 private static List<File> flvList = new ArrayList<>(); 4 5 public static void main(String[] args) { 6 // 視頻的下載路徑 7 String downloadPath = "E:\\Videos\\Bilibili videos\\75233634"; 8 // 視頻av號:就是路徑的最后一級目錄 9 String avNum = null; 10 Pattern pattern = Pattern.compile("\\d+"); 11 Matcher matcher = pattern.matcher(downloadPath); 12 if (matcher.find()) { 13 avNum = matcher.group(); 14 } 15 16 List<File> infoList = getInfoList(downloadPath); 17 List<File> flvList = getFlvList(downloadPath); 18 List<String> partNameList = getPartNameList(infoList); 19 20 for (int i = 0; i < flvList.size(); i++) { 21 // System.out.println(flvList.get(i)); 22 // System.out.println(getDestFile(partNameList.get(i), flvList.get(i), avNum)); 23 String partName = partNameList.get(i); 24 File flvFile = flvList.get(i); 25 // 目標文件:E:\Videos\75233634\1_1、這階段該如何學習.flv 26 File destFile = getDestFile(partName, flvFile, avNum); 27 //原始文件:E:\Videos\Captures\75233634\1\75233634_1_0.flv 28 File originFile = flvList.get(i); 29 if (originFile.renameTo(destFile)) { 30 System.out.println("success!" + destFile.getName()); 31 } 32 } 33 }
代碼已上傳:Github
結果
結果當然成功了,這肯定毋庸置疑了。
終於可以愉快的看視頻了...😆
堅定而緩慢地做自己力所能及的事。
時間
2019-12-12
地點
zut.north#5 2f
人物
me