git
昨天晚上從B站電腦客戶端下了一個分集視頻程序員
可是下載後的視頻是這樣的:github
視頻名是這樣的:數組
這樣既不直觀又不美觀,就算把視頻文件放到一個文件夾內,連續看視頻時也不容易記住看到哪一個。因此就有了今天的事情。微信
起初,個人想法是複製出來一個一個該文件名,可是當時我想到,做爲一個優秀的程序員,怎麼能幹這種無腦的活呢?😆(實際上是太懶了。。。)ide
因而,,,就去百度了,百度確實找到一個,可是上面的方法根本沒有寫全,雖然我已經盡力補全,可是他仍是漏了一個很是關鍵的一步,因此我就決定本身來寫。學習
根據下載的文件來看,在info文件中,能夠找到原視頻的全部介紹信息,其中固然就有原視頻的文件名(PartName),因此咱們只須要把info文件中的文件名提取出來,而後再對flv視頻文件重命名不就完事了嗎。spa
具體能夠分爲如下步驟:code
遍歷咱們下載好的視頻目錄,獲得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_一、這階段該如何學習.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_一、這階段該如何學習.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