2013-04-06 68 views
0

所以我已經從Coursera下載了「Introduction to computer networks」課程的mp4和srt文件。但mp4和srt文件的名稱之間略有差異。如何編寫一個程序來重命名mp4文件以匹配srt文件的名稱?

文件名樣本如下:

1 - 1 - 1-1 Goals and Motivation (1253).mp4 
1 - 1 - 1-1 Goals and Motivation (12_53).srt 
1 - 2 - 1-2 Uses of Networks (1316).mp4 
1 - 2 - 1-2 Uses of Networks (13_16).srt 
1 - 3 - 1-3 Network Components (1330).mp4 
1 - 3 - 1-3 Network Components (13_30).srt 
1 - 4 - 1-4 Sockets (1407).mp4 
1 - 4 - 1-4 Sockets (14_07).srt 
1 - 5 - 1-5 Traceroute (0736).mp4 
1 - 5 - 1-5 Traceroute (07_36).srt 
1 - 6 - 1-6 Protocol Layers (2225).mp4 
1 - 6 - 1-6 Protocol Layers (22_25).srt 
1 - 7 - 1-7 Reference Models (1409).mp4 
1 - 7 - 1-7 Reference Models (14_09).srt 
1 - 8 - 1-8 Internet History (1239).mp4 
1 - 8 - 1-8 Internet History (12_39).srt 
1 - 9 - 1-9 Lecture Outline (0407).mp4 
1 - 9 - 1-9 Lecture Outline (04_07).srt 
2 - 1 - 2-1 Physical Layer Overview (09_27).mp4 
2 - 1 - 2-1 Physical Layer Overview (09_27).srt 
2 - 2 - 2-2 Media (856).mp4 
2 - 2 - 2-2 Media (8_56).srt 
2 - 3 - 2-3 Signals (1758).mp4 
2 - 3 - 2-3 Signals (17_58).srt 
2 - 4 - 2-4 Modulation (1100).mp4 
2 - 4 - 2-4 Modulation (11_00).srt 
2 - 5 - 2-5 Limits (1243).mp4 
2 - 5 - 2-5 Limits (12_43).srt 
2 - 6 - 2-6 Link Layer Overview (0414).mp4 
2 - 6 - 2-6 Link Layer Overview (04_14).srt 
2 - 7 - 2-7 Framing (1126).mp4 
2 - 7 - 2-7 Framing (11_26).srt 
2 - 8 - 2-8 Error Overview (1745).mp4 
2 - 8 - 2-8 Error Overview (17_45).srt 
2 - 9 - 2-9 Error Detection (2317).mp4 
2 - 9 - 2-9 Error Detection (23_17).srt 
2 - 10 - 2-10 Error Correction (1928).mp4 
2 - 10 - 2-10 Error Correction (19_28).srt 

我想重命名的MP4文件匹配SRT文件,所以當我玩的影片,VLC可以自動加載字幕。什麼人討論算法來做到這一點?您也可以使用任何語言提供解決方案代碼,因爲我熟悉許多編程語言。但是python和C++更可取。

編輯: 感謝所有回覆的人。我知道重命名srt文件比其他方式更容易。但我認爲重命名mp4文件會更有趣。有什麼建議麼?

+1

錯誤,只是['glob'](http://docs.python.or g/2/library/glob.html)所有'.str'並刪除下劃線?這就像十條蟒蛇線一樣。 – Zeta 2013-04-06 07:41:07

+0

該算法很簡單:對於每個mp4文件,找到其相應的srt文件(1),並將其重命名爲mp4文件具有的同名文件,但使用另一個擴展名。你應該繼續嘗試。 (1)不應該很難,因爲您在文件名的第一個字段中具有識別信息。 – Mariano 2013-04-06 07:47:34

回答

2

這裏是Python中的快速解決方案。

的工作很簡單,如果你做如下假設:

  1. 所有文件都在同一文件夾
  2. 您在目錄中有
  3. 所有SRT是相同數量的SRT和MP4文件按字母順序排列,所有mp4均按字母順序排列

注意我不假設任何關於實際名稱(例如只需要刪除下劃線)的內容。

所以,你不需要任何特殊的邏輯來匹配文件,只需一個接一個。

import os, sys, re 
from glob import glob 

def mv(src, dest): 
    print 'mv "%s" "%s"' % (src, dest) 
    #os.rename(src, dest) # uncomment this to actually rename the files 

dir = sys.argv[1] 

vid_files = sorted(glob(os.path.join(dir, '*.mp4'))) 
sub_files = sorted(glob(os.path.join(dir, '*.srt'))) 
assert len(sub_files) == len(vid_files), "lists of different lengths" 

for vidf, subf in zip(vid_files, sub_files): 
    new_vidf = re.sub(r'\.srt$', '.mp4', subf) 
    if vidf == new_vidf: 
     print '%s OK' % (vidf,) 
     continue 
    mv(vidf, new_vidf) 

再次,這只是一個快速的腳本。改進建議:

  1. 支持不同的文件擴展
  2. 使用更好的CLI,例如​​
  3. 支持以多個目錄
  4. 支持測試模式(實際上不重命名文件)
  5. 更好的錯誤報告(而不是使用assert
  6. 更先進:支持撤銷
+0

似乎OP想讓'mp4's改名而不是'srt'。我編輯了你的答案,因爲它是最適合這個任務的。 – Zeta 2013-04-06 09:06:35

+0

謝謝shx2和@Zeta。這個腳本完全符合我的要求,並且非常感謝你提出的改進建議。我會盡力在稍後做出改進。 – ikhthiandor 2013-04-06 15:26:33

3

如果所有這些文件確實按照這個方案的Python實現幾乎是微不足道的:

import glob, os 

for subfile in glob.glob("*.srt") 
    os.rename(subfile, subfile.replace("_","")) 

如果你的MP4中還含有強調要添加一個額外的循環他們。

2

for f in *.srt; do mv $f ${f%_}; done

+0

雖然有效,但只適用於類似bash的環境。它不能在Windows上使用:(。 – Zeta 2013-04-06 08:54:59

1

這只是Zeta說的一個實現:)

import os; 
from path import path; 
for filename in os.listdir('.'):   
    extension = os.path.splitext(path(filename).abspath())[1][1:] 
    if extension == 'srt':   
     newName = filename.replace('_',''); 
     print 'Changing\t' + filename + ' to\t' + newName; 
     os.rename(filename,newName); 
print 'Done!' 
相關問題