Last active
May 13, 2021 01:32
-
-
Save hugoleodev/b81803041a38a53826c7163ab126ceb5 to your computer and use it in GitHub Desktop.
Splits videos based on its subtitle file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any, Generator, TextIO | |
import ffmpeg | |
from srt import Subtitle | |
from srt import parse as srt_parse | |
def segments(file: TextIO) -> Generator: | |
return srt_parse(file.read()) | |
def split_video(video_path: str, subtitle: Subtitle) -> Any: | |
segment_start: int = subtitle.start.total_seconds() | |
segment_end: int = subtitle.end.total_seconds() | |
duration: int = segment_end - segment_start | |
return ( | |
ffmpeg.input(video_path, ss=segment_start, t=duration) | |
.output(f"chunk_{subtitle.index}.mp4", vf="scale=320:240") | |
.run() | |
) | |
if __name__ == '__main__': | |
episode_path = "Friends S01E01 The One Where Monica Gets a New Roomate (1080p x265 Joy).mp4" | |
subtitle_path = "Friends Season 01 Episode 01 - Pilot.srt" | |
with open(subtitle_path, "r") as subtitle_file: | |
for segment in segments(subtitle_file): | |
split_video(episode_path, segment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment