c++ - How to write a program to rename mp4 files to match the name of srt files? -
so have downloaded mp4 , srt files "introduction computer networks" course coursera. there slight discrepancy between names of mp4 , srt files.
the file name samples following:
1 - 1 - 1-1 goals , motivation (1253).mp4 1 - 1 - 1-1 goals , 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 i want rename mp4 files match srt files vlc can automatically load subtitles when play videos. discuss algorithms this? can provide solution code in language familiar many programming languages. python , c++ preferable.
edit: replied. know easier rename srt files other way around. think more interesting rename mp4 files. suggestions?
here's quick solution in python.
the job simple if make following assumptions:
- all files in same folder
- you have same number of srt , mp4 files in directory
- all srt ordered alphabetically, mp4 ordered alphabetically
note not assume actual names (e.g. need remove underscores).
so don't need special logic matching files, go one-by-one.
import os, sys, re glob import glob def mv(src, dest): print 'mv "%s" "%s"' % (src, dest) #os.rename(src, dest) # uncomment rename 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" 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) again, quick script. suggested improvements:
- support different file extensions
- use better cli, e.g.
argparse - support taking multiple directories
- support test-mode (don't rename files)
- better error reporting (instead of using
assert) - more advanced: support undoing
Comments
Post a Comment