调整srt字幕文件的时间轴
August 18th, 2008 | by vvoody |从BT上下了部电影,又从射手下了字幕,播放是发现字幕比声音晚了大概8、9秒。当然,mplayer可以使用参数”-subdelay 9″来达到目的,可我还是希望能直接把字幕文件调整好。网上搜了一下,字幕调解软件还是有不少,都是GUI的。对我而言只要调解时间轴就够了,所以命令行的比较好,于是自己写了个python的脚本。
这个脚本仅是简单的对字幕中的时间轴进行加或减,以达到调解时间轴的目的。不熟悉python的库让我兜了一大圈,本以为datetime不能处理时间的加减,后来才发现原来可以!对OO的不熟又让我被datetime类搞的小晕了一下。
用的时候执行:python adjust_srt_timeline.py your_movie.srt delay_time
your_movie.srt 就是电影的字幕文件,delay_time则是你想字幕时间轴延迟的时间。比如,字幕比声音晚了8.5秒,那么delay_time就是 -8.5。
效果图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #!/usr/bin/python # -*- coding: utf-8 -*- # # Some srt subtitile is slower or faster. This little script can adjust it. # # Usage: python adjust_srt_timeline.py your_movie.srt delay_second # e.g. python adjust_srt_timeline.py Shortbus.chs.srt -8.5 # # Then there will be a "Shortbus.chs.srt_new" file. # # Copyright (C) 2008, vvoody <wxj.g.sh{AT}gmail.com> # GPLv3 applies ;-) import sys, re, datetime def get_new_timeline(old_timeline, sec, microsec): '''Add or subtract the timeline and return a new timeline string. This function can receive the argument like: 00:12:59,123 or 00:12:59.123 Return the new one like: 00:13:01,583 You can substitute "." for "," manually after returning. ''' hour, minute, second, millisecond = \ map(int, (re.split(r'[:,\.]', old_timeline) )) # We must set the year, month, day. It cannont be left none. new_timeline = datetime.datetime(2000, 1, 1, hour, minute, second, millisecond*1000) + \ datetime.timedelta(seconds=sec, microseconds=microsec) return '%02d:%02d:%02d,%03d' % \ (new_timeline.hour, new_timeline.minute, new_timeline.second, new_timeline.microsecond/1000) try: old_srt_name = sys.argv[1] old_srt = open(old_srt_name, "r") except IOError: print >> sys.stderr, "Failed to open your srt file!\n" try: new_srt_name = sys.argv[1] + "_new" new_srt = open(new_srt_name, "w") except IOError: print >> sys.stderr, "Failed to create a new srt file!\n" print "Start..." # Support .000000 ~ .999999, but .0 ~ .9 is enough. delay = (float)(sys.argv[2]) second = (int)(delay) microsecond = (int)(1000000*(delay - second)) # A timeline is like -> "00:12:59,123 -> 00:13:00,291" modetext = re.compile(r"(\d{2}:\d{2}:\d{2}[.,]\d{3}).*(\d{2}:\d{2}:\d{2}[.,]\d{3})") for line in old_srt: if re.match(modetext, line): timeline_start, timeline_end = re.match(modetext, line).groups() new_timeline_start = get_new_timeline( timeline_start, second, microsecond ) new_timeline_end = get_new_timeline( timeline_end, second, microsecond ) line = line.replace( timeline_start, new_timeline_start ) line = line.replace( timeline_end, new_timeline_end ) # Print a dot when one line adjusted. print '.', new_srt.write(line) # End of FOR print print "Adjust successfully!" old_srt.close() new_srt.close() # End of script. |
3 Responses to “调整srt字幕文件的时间轴”
By ghosTM55 on Aug 20, 2008
Mozilla 1.9.0.1 Linux Mozilla 1.9.0.1 Linux顶你个肺阿
Reply
By vvoody on Aug 20, 2008
Opera 9.52 Linux Opera 9.52 Linuxlol,吓我一跳!
Reply
By bitstream on Aug 22, 2008
Mozilla Firefox 3.0.1 Fedora Linux Mozilla Firefox 3.0.1 Fedora Linuxlooks very nice~~!
Reply