Giter Site home page Giter Site logo

swapsub's Introduction

  • 👋 Hi, I’m @cybytess
  • 👀 I’m interested in python and HTML
  • 🌱 I’m currently learning.

👀 I want to find some useful project

swapsub's People

Contributors

cybytess avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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("转化成功")

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.