Giter Club home page Giter Club logo

swapsub's Issues

bug修复并合并为单脚本:UnboundLocalError: local variable 'hh_list' referenced before assignment

模块load函数里,关于srt的判断语句中,finallist列表和返回值的缩进写错了,会出现局部变量提前引用的错误。
个人已修复,代码为:


"""
功能说明:
字幕文件格式转换器,支持LRC歌词,SRT视频字幕,TXT文本三者互转
注意 mac和 win 的路径格式,为避免错误,最后和脚本放在同一目录使用。

原始参考:
安装字幕转化包:pip install swapsub (有 bug)
(pip发布的脚本存在问题,同时下载下来的脚本,局部变量缩进不对,已优化合并为一个脚本)
https://github.com/cybytess/swapsub

"""

# 载入字幕歌词文件,根据后缀判断,srt,或者lrc。
def load(path):
    if path.split(".")[len(path.split(".")) - 1] == "lrc":
        hh_list = []
        mm_list = []
        ss_list = []
        mms_list = []
        content_list = []
        file = open(path, "r", encoding="utf-8")
        sub = file.readlines()
        sub[0] = sub[0][1:len(sub[0]) + 1]
        file.close()
        # print(sub)
        for n in sub:
            n = n.strip(" ")
            n = n.strip("[")
            content = n.split("]")[1]
            time = n.split("]")[0]
            hh_list.append(str(int(time.split(':')[0]) // 60).rjust(2, '0'))
            mm_list.append(str(int(time.split(':')[0]) % 60).rjust(2, '0'))
            ss_list.append(str(time.split(':')[1].split('.')[0]).rjust(2, '0'))
            mms_list.append(str(time.split(':')[1].split('.')[1]).ljust(3, '0'))
            content_list.append(content)
        finallist = [hh_list, mm_list, ss_list, mms_list, content_list]
        # print("lrc格式")
        return (finallist)

    if path.split(".")[len(path.split(".")) - 1] == "srt":
        hh_list = []
        mm_list = []
        ss_list = []
        mms_list = []
        content_list = []
        file = open(path, "r", encoding="utf-8")
        sub = file.readlines()
        sub[0] = str(0)
        file.close()
        # print(sub)
        a = 0
        for n in sub:
            n = n.strip('\n')
            if n.isdecimal():
                time = sub[int(a) + 1].split(' --> ')[0]
                hh = str((time.split(':')[0]))
                mm = str((time.split(':')[1]))
                ss = str(time.split(',')[0].split(':')[2])
                mms = str(time.split(',')[1])
                content = sub[a + 2]
                hh_list.append(hh)
                mm_list.append(mm)
                ss_list.append(ss)
                mms_list.append(mms)
                content_list.append(content)
                # print(hh_list,mm_list,ss_list,mms_list,content_list)
            a = a + 1
        finallist = [hh_list, mm_list, ss_list, mms_list, content_list]
        # print("srt格式")
        return (finallist)

# 转化出的格式,根据设置好的后缀名,转化格式文件。只支持,字幕srt,lrc转化为,TXT,srt,lrc等格式
def convert(data, path):
    if path.split(".")[len(path.split(".")) - 1] == "txt":
        content_list = data[4]
        file = open(path, "w", encoding="utf-8")
        for a in content_list:
            file.write(a)
        file.close()

    if path.split(".")[len(path.split(".")) - 1] == "lrc":
        hh_list = data[0]
        mm_list = data[1]
        ss_list = data[2]
        mms_list = data[3]
        content_list = data[4]
        file = open(path, "w", encoding="utf-8")
        for a in range(len(mm_list)):
            file.write('[' + mm_list[a] + ':' + ss_list[a] + '.' + mms_list[a] + ']' + content_list[a])
        file.close()

    if path.split(".")[len(path.split(".")) - 1] == "srt":
        hh_list = data[0]
        mm_list = data[1]
        ss_list = data[2]
        mms_list = data[3]
        content_list = data[4]
        file = open(path, "w", encoding="utf-8")
        hh_list.append(hh_list[len(hh_list) - 1])
        mm_list.append(mm_list[len(mm_list) - 1])
        ss_list.append(int(ss_list[len(ss_list) - 1]) + 5)
        mms_list.append(mms_list[len(mms_list) - 1])
        for a in range(len(hh_list) - 1):
            file.write(str(a) + '\n')
            file.write(str(hh_list[a]) + ':' + str(mm_list[a]) + ':' + str(ss_list[a]) + ','
                       + str(mms_list[a]) + " --> " + str(hh_list[a + 1]) + ':' + str(mm_list[a + 1])
                       + ':' + str(ss_list[a + 1]) + ',' + str(mms_list[a + 1]) + '\n')
            file.write(content_list[a] + '\n')
        file.close()

if __name__ == '__main__':
    file_path = "test.srt"
    date = load(file_path)
    convert(date,file_path)
    print("转化成功")

轉換出來沒有字幕

00:02:16,600 --> 00:02:20,610
[00:51.29
19
00:02:20,610 --> 00:02:21,960
[00:55.28
20
00:02:21,960 --> 00:02:25,970
[00:56.64
21
00:02:25,970 --> 00:02:27,250
[01:00.65
22
00:02:27,250 --> 00:02:30,790
[01:01.95
23
00:02:30,790 --> 00:02:31,310
[01:05.52
24
00:02:31,310 --> 00:03:39,560
[01:05.99
25
00:03:39,560 --> 00:03:43,240
[02:54.66
26
00:03:43,240 --> 00:03:43,960
[02:57.59
27
00:03:43,960 --> 00:03:44,470
[02:58.61
28
00:03:44,470 --> 00:03:48,570
[02:59.10
29
00:03:48,570 --> 00:03:49,950
[03:03.30
30
00:03:49,950 --> 00:03:53,910
[03:27.58
31
00:03:53,910 --> 00:03:55,320
[03:30.02
32
00:03:55,320 --> 00:03:58,710
[03:31.34
33
00:03:58,710 --> 00:03:59,250
[03:34.64
34
00:03:59,250 --> 00:02:53,530
[03:35.31
35
00:02:53,530 --> 00:02:58,530
[01:28.29

===========
[02:20.61][00:55.28]
[02:21.96][00:56.64]...
[02:25.97][01:00.65]
[02:27.25][01:01.95]...
[02:30.79][01:05.52]...
[02:31.31][01:05.99]

這種格式的

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.