栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python将lrc歌词转换为srt字幕格式

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python将lrc歌词转换为srt字幕格式

使用Python将lrc歌词转换为srt字幕格式

思路

比较简单的文本处理问题
主要注意点:
1. 存在空白时间和无空白时间两种形式的lrc文件,需要分别处理
2. 最后一句的时间段需要单独计算

源码 util.py
# -*- coding: utf-8 -*-

"""
@author: WUNNAN

@Created on: 2022/7/28 20:38
"""


# str -> int
def convert_str(time):
    a, f = map(str, time.split("."))
    m, s = map(int, a.split(":"))
    f = int(f)*10
    return m, s, f


# 普通位置歌词时间计算
def cal_time(time):
    m, s, f = convert_str(time)
    if f-50 < 0:
        s -= 1
        if s < 0:
            m -= 1
        else:
            f += 950
    return m, s, f


# 最后一句时间计算 默认1s
def cal_last_time(m, s, f):
    if s+10 > 60:
        m += 1
        s -= 50
    else:
        s += 10
    return m, s, f


# 处理最后一句
def deal_last_lyric(last):
    last_time = last[0].replace("[", "")
    last_lyric = last[1]
    m1, s1, f1 = convert_str(last_time)
    m2, s2, f2 = cal_last_time(m1, s1, f1)
    last_period = "00:%02d:%02d,%03d --> 00:%02d:%02d,%03d" % (m1, s1, f1, m2, s2, f2)
    return [last_period, last_lyric]


# 处理普通歌词
def deal_common_lyric(blocks_time, blocks_lyrics):
    tmp = []
    for i in range(len(blocks_time)):
        tmp += deal_time(blocks_time[i], blocks_lyrics[i])
    return tmp


# 普通歌词 分块处理
def deal_time(block_time, block_lyrics):
    tmp = []
    for i in range(len(block_time)-1):
        m1, s1, f1 = convert_str(block_time[i])
        m2, s2, f2 = cal_time(block_time[i+1])
        period = "00:%02d:%02d,%03d --> 00:%02d:%02d,%03d"%(m1, s1, f1, m2, s2, f2)
        tmp.append([period, block_lyrics[i]])
    return tmp


# 整理 写入
def prepare_and_write(tmp, LRC_path):
    res = []
    for i in range(len(tmp)):
        res.append(str(i + 1) + "n")
        res.append(tmp[i][0] + "n")
        res.append(tmp[i][1] + "n")
        res.append("n")
    SRT_path = LRC_path.replace("lrc", "srt")
    with open(SRT_path, "w+", encoding="utf-8") as file:
        file.writelines(res)
    print("转换完成!n")

main.py
# -*- coding: utf-8 -*-

"""
@author: WUNNAN

@Created on: 2022/7/28 20:37
"""
from util import *


def start_deal(file):
    times = []
    lyrics = []
    for sen in file[:-1]:
        t, l = map(str, sen.split("]"))
        times.append(t.replace("[", ""))
        lyrics.append(l)

    # 最后一句
    last = file[-1].split("]")
    # 其余歌词
    blocks_lyrics = []
    blocks_time = []
    start = 0
    flag = False
    for i in range(len(lyrics) - 1):
        if len(lyrics[i + 1]) == 0:
            flag = True
            # 以空白时间为分割线,构造出一个二维列表
            blocks_lyrics.append(lyrics[start: i + 1])
            blocks_time.append(times[start: i + 2])
            start = i + 2
    if flag:
        tmp = deal_common_lyric(blocks_time, blocks_lyrics)
    else:
        tmp = deal_time(times, lyrics)

    tmp.append(deal_last_lyric(last)) # 添加最后一句

    return tmp



if __name__ == '__main__':
    while True:
        path = input("请输入LRC文件路径:n").replace("\", "//")
        try:
            file = open(path, encoding="utf-8").read().split("n")
            tmp = start_deal(file)
            prepare_and_write(tmp, path)
        except:
            print("读取文件失败!n")
视频教程

lrc歌词到srt字幕的转换程序

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029548.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号