This article is the source code of the following articles.
Set the color on the poster side so that the color of the Youtube subtitles changes automatically.
If you can edit it, feel free to use it.
――Originally, I didn't intend to publish it so much, so the decipherability is quite bad. I've added comments, but it may not be helpful. ――Programming itself is an amateur, and there may be some mistakes in writing. (I think it will work) ――How to use is written in the above article. If you are interested, I would be happy if you could take a look. ――If you have any improvements, I would appreciate it if you could comment.
A configuration file etc. is required for operation, so please download it from the above article. Simply put, it's like converting a subtitle file that can be downloaded from Youtube into a format that can describe a color code, and adding the specified color code. Since the unit of time for the subtitle file downloaded from Youtube and the converted file is different, the first half is messed up. I use a lot of "+", but now that I think about it, I should have used ".format ()".
Colored subtitle creation tool.py
# -*- coding: utf-8 -*-
import os
import zipfile
import sys
from time import sleep
SetRoot = "./setting file/"
CapRoot = "./Please put the downloaded subtitle file in this./"
#---Color code dictionary type creation based on definition file------------------------------------------------------------
ReadLineNum = 0
try:
with open(SetRoot + "Color to use and specified character.txt",'r',encoding="utf-8") as d:
ColorCode = [s.strip() for s in d.readlines()]
except FileNotFoundError:
print(""Colors to use and specified characters.The "txt" file does not exist. Please create or download again.")
print("Press Enter to exit.")
k = input()
sys.exit()
ColorCodeLineEnd = len(ColorCode) - 1
ColorCodeDic = {str(ColorCode[ReadLineNum].split(":")[0]):str(ColorCode[ReadLineNum].split(":")[1])}
while True:
if ColorCodeLineEnd == ReadLineNum:
break
else:
ReadLineNum += 1
ColorCodeDic.update({str(ColorCode[ReadLineNum].split(":")[0]):str(ColorCode[ReadLineNum].split(":")[1])})
#-----------------------------------------------------------------------------------------------------
#---Convert Captions file to list type, timecode to milliseconds, then save as RawCode in list type------------------
try:
with open(CapRoot + "captions.sbv",'r',encoding="utf-8") as f:
Captions = [s.strip() for s in f.readlines()]
FileType = 1
except FileNotFoundError:
try:
with open(CapRoot + "captions.srt",'r',encoding="utf_8_sig") as f:
Captions = [s.strip() for s in f.readlines()]
FileType = 2
except FileNotFoundError:
print("「captions."sbv" or "captions".The "srt" file does not exist. Check if the file format and name match.")
print("Press Enter to exit.")
k = input()
sys.exit()
CaptionsLineEnd = len(Captions) - 1
ReadLineNum = 0
RawCode = ["Code"]
if FileType == 1:
while True:
if Captions[ReadLineNum][1:2] == ":" and Captions[ReadLineNum][13:14] == ":":
start = Captions[ReadLineNum][0:11].split(":")
a = start[2].split(".")
start = int(start[0]) * 3600000 + int(start[1]) * 60000 + int(a[0]) * 1000 + int(a[1])
RawCode.append(str(start))
end = Captions[ReadLineNum][12:23].split(":")
a = end[2].split(".")
end = int(end[0]) * 3600000 + int(end[1]) * 60000 + int(a[0]) * 1000 + int(a[1])
RawCode.append(str(end))
elif Captions[ReadLineNum] == "":
pass
else:
RawCode.append(Captions[ReadLineNum])
if ReadLineNum == CaptionsLineEnd:
break
else:
ReadLineNum += 1
elif FileType == 2:
while True:
if Captions[ReadLineNum].isnumeric() == True:
ReadLineNum += 1
start = Captions[ReadLineNum][0:12].split(":")
a = start[2].split(",")
start = int(start[0]) * 3600000 + int(start[1]) * 60000 + int(a[0]) * 1000 + int(a[1])
RawCode.append(str(start))
end = Captions[ReadLineNum][17:29].split(":")
a = end[2].split(",")
end = int(end[0]) * 3600000 + int(end[1]) * 60000 + int(a[0]) * 1000 + int(a[1])
RawCode.append(str(end))
elif Captions[ReadLineNum] == "":
pass
else:
RawCode.append(Captions[ReadLineNum])
if ReadLineNum == CaptionsLineEnd:
break
else:
ReadLineNum += 1
#-----------------------------------------------------------------------------------------------------
#---Reshape RawCode and save as EditCode in list type----------------------------------------------------
RawCodeLineEnd = len(RawCode) - 1
ReadLineNum = 1
EditCode = ["EditCode"]
Break = 0
CountSentence = 0
while True:
EditCode.append(RawCode[ReadLineNum])
ReadLineNum += 1
end = RawCode[ReadLineNum]
ReadLineNum += 1
word = RawCode[ReadLineNum]
while True:
if ReadLineNum == RawCodeLineEnd:
EditCode.append(word)
CountSentence += 1
Break = 1
break
if RawCode[ReadLineNum].isnumeric() == False and RawCode[ReadLineNum + 1].isnumeric() == False: #If there is a line break in the subtitle, add it including the line break code
word = word + "</b><br><b>" + RawCode[ReadLineNum + 1]
ReadLineNum += 1
else:
EditCode.append(word)
CountSentence += 1
break
if Break == 1:
break
ReadLineNum += 1
if RawCode[ReadLineNum] == end:
pass
else: #When the start time of the subtitle to be displayed and the end time of the previous subtitle do not match
EditCode.append(end) #Create a character string to be displayed from the end time of the previous subtitle
EditCode.append(" ") #Substitute a character string that indicates that it will not be displayed
#-----------------------------------------------------------------------------------------------------
#---Create a list of color information for each subtitle based on the color specification file--------------------------------------------------
try:
with open(SetRoot + "Color specification file.txt",'r',encoding="utf-8") as n:
ColorCodeRaw = [s.strip() for s in n.readlines()]
except FileNotFoundError:
print(""Color specification file.The "txt" file does not exist. Please create or download again.")
print("Press Enter to exit.")
k = input()
sys.exit()
ColorCodeLineMax = len(ColorCodeRaw) - 1
ReadLineNum = 0
ColorCode = ["ColorCode"]
if ColorCodeRaw[0][0:3] == "all":
AllSameColor = 1
else:
AllSameColor = 0
while True:
if ReadLineNum < CountSentence:
if AllSameColor == 1:
ColorCode.append(str(ColorCodeRaw[0].split(" ")[1]))
elif ColorCodeLineMax < ReadLineNum:
ColorCode.append(str(ColorCodeRaw[ColorCodeLineMax]))
else:
ColorCode.append(str(ColorCodeRaw[ReadLineNum]))
ReadLineNum += 1
elif ReadLineNum == CountSentence:
break
#-----------------------------------------------------------------------------------------------------
#---Ask for input---------------------------------------------------------------------------------------
FileNameDic = {"1":"captions.sbv","2":"captions.srt"}
print("Imported subtitle file: " + FileNameDic[str(FileType)])
print("Available colors: " + str(ColorCodeLineEnd + 1) + "color")
if AllSameColor == 1:
w = "Same color (" + ColorCodeDic[ColorCode[1]] + ")\n"
else:
w = "Change by sentence\n"
print("Color specification mode: " + w)
print("The required files have been read. From now on, we will create a subtitle file.")
print("Check if the above read contents are correct, and if you want to start creating, "y"], To quit, type "n" and press Enter.")
key = input()
if key == "y":
print("Enter the name of the file where you want to save the created data. No extension is required.(Press Enter after typing)")
FileName = input()
if FileName == "":
print(""Color Subtitles" because it was not entered.Save as "smi".")
FileName = "ColorSubtitles.smi"
else:
print("「" + FileName + ".Save as "smi".")
FileName = FileName + ".smi"
print("Press Enter to start working.")
k = input()
else:
print("I stopped working. Press Enter to exit.")
k = input()
sys.exit()
#-----------------------------------------------------------------------------------------------------
#---Create a file based on EditCode-----------------------------------------------------------------------
SentenceLineEnd = len(EditCode) - 1
ReadLineNum = 1
Sentence = ["<SAMI>\n<HEAD>\n<SAMIParam>\nMetrics {time:ms;}\nSpec {MSFT:1.0;}\n</SAMIParam>\n</HEAD>\n<BODY>\n"]
SentenceNum = 1
while True:
Sentence.append('<SYNC Start=' + str(EditCode[ReadLineNum]) + '>')
print('<SYNC Start=' + str(EditCode[ReadLineNum]) + '>')
ReadLineNum += 1
if EditCode[ReadLineNum] == " ":
Sentence.append(' \n')
print(' ')
else:
Sentence.append('<font color="' + ColorCodeDic[ColorCode[SentenceNum]] + '"><b>' + str(EditCode[ReadLineNum]) + '</b></font>\n')
print('<font color="' + ColorCodeDic[ColorCode[SentenceNum]] + '"><b>' + str(EditCode[ReadLineNum]) + '</b></font>')
SentenceNum += 1
if ReadLineNum == SentenceLineEnd:
break
else:
ReadLineNum += 1
sleep(0.01)
with open("./" + FileName, mode='a',encoding="utf-8") as f:
f.writelines(Sentence)
#-----------------------------------------------------------------------------------------------------
print("\n\n Export work is complete. "" + FileName + "I saved it as.")
print("Press Enter to exit.")
k = input()
sys.exit()
All subtitles are set to be bold. If you want to change it, delete the \ and \ </ b> tags at the bottom of the script, "Create a file based on EditCode". You may copy the following excerpt to the relevant part.
Bottom excerpt of script
Colored subtitle creation tool.py
#---Create a file based on EditCode-----------------------------------------------------------------------
SentenceLineEnd = len(EditCode) - 1
ReadLineNum = 1
Sentence = ["<SAMI>\n<HEAD>\n<SAMIParam>\nMetrics {time:ms;}\nSpec {MSFT:1.0;}\n</SAMIParam>\n</HEAD>\n<BODY>\n"]
SentenceNum = 1
while True:
Sentence.append('<SYNC Start=' + str(EditCode[ReadLineNum]) + '>')
print('<SYNC Start=' + str(EditCode[ReadLineNum]) + '>')
ReadLineNum += 1
if EditCode[ReadLineNum] == " ":
Sentence.append(' \n')
print(' ')
else:
Sentence.append('<font color="' + ColorCodeDic[ColorCode[SentenceNum]] + '">' + str(EditCode[ReadLineNum]) + '</font>\n')
print('<font color="' + ColorCodeDic[ColorCode[SentenceNum]] + '">' + str(EditCode[ReadLineNum]) + '</font>')
SentenceNum += 1
if ReadLineNum == SentenceLineEnd:
break
else:
ReadLineNum += 1
sleep(0.01)
with open("./" + FileName, mode='a',encoding="utf-8") as f:
f.writelines(Sentence)
#-----------------------------------------------------------------------------------------------------
print("\n\n Export work is complete. "" + FileName + "I saved it as.")
print("Press Enter to exit.")
k = input()
sys.exit()
If you rewrite the tag part, it can be displayed differently. (The tag b and the tag u are bold and underlined, respectively) It will be applied to all the subtitles to be displayed, but ... Thank you for the information.
I referred to the following article for exe conversion.
Recommended Posts