챗지피티와 간단한 어플 만들기
자막 파일을 일반 텍스트 파일로 만드는 것이 생각보다 어렵네요. 작업은 매우 간단한데 마음에 드는 툴이 없어서 하나 만들어 사용하기로 했습니다.
챗지피티를 시켜서 파이썬으로 만들었는데, 엄청난 속도로 깔끔하게 처리해 주네요. 아주 마음에 들어요. 좀 더 손질하면 앱스토어에 올려도 될 듯합니다.
srt 자막파일에서 빈줄 타임라인 번호 등을 제외한 대사가 있는 줄만 뽑아서 텍스트파일로 만드는 파이썬코드를 만들자
아래는 SRT 자막 파일에서 빈 줄, 타임라인 번호 등을 제외하고 대사만 추출하여 텍스트 파일로 저장하는 파이썬 코드입니다.
우분투에서 실행하는 방법
우분투에서 위의 파이썬 코드를 실행하는 방법은 다음과 같습니다:
srt 자막파일에 중복되는 내용 들어가네.. 같은 내용의 대사도 정리하도록 수정해줘
중복된 대사를 제거하고, 같은 내용의 대사가 반복되지 않도록 수정된 파이썬 코드를 아래와 같이 작성할 수 있습니다. 이를 통해 같은 대사가 반복되면 한 번만 추출되도록 처리합니다.
좋아, 잘했어
고마워요! 도움이 되어 기쁩니다. 다른 질문이나 도움이 필요하면 언제든지 말씀해주세요! 😊
def extract_dialogue_from_srt(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as infile:
lines = infile.readlines()
dialogue_lines = []
seen_lines = set() # 이미 본 대사는 저장하여 중복 제거
for line in lines:
# Remove lines that are empty or represent timecodes
if line.strip() == "" or "-->" in line:
continue
# Ignore lines that are numeric (timeline numbers)
if line.strip().isdigit():
continue
# Remove duplicate dialogue
stripped_line = line.strip()
if stripped_line and stripped_line not in seen_lines:
dialogue_lines.append(stripped_line)
seen_lines.add(stripped_line)
# Save dialogue lines to the output file
with open(output_file, 'w', encoding='utf-8') as outfile:
outfile.write("\n".join(dialogue_lines))
print(f"Dialogue lines extracted and saved to {output_file}")
# Example usage
input_srt = 'input.srt' # Replace with your SRT file path
output_txt = 'output.txt' # Replace with your desired output file path
extract_dialogue_from_srt(input_srt, output_txt)
사용하시는 방법은 이 어플과 자막을 같은 폴더에 둡니다. 그리고 자막화일을 'input.srt' 로 이름을 바꾸어 줍니다.
python3 extract_dialogue.py
이렇게 실행하시면 내용이 깨끗이 정리된 텍스트 화일이 'output.txt' 로 생겨났을 겁니다.