m1guelpf / yt-whisper

Using OpenAI's Whisper to automatically generate YouTube subtitles
MIT License
1.36k stars 138 forks source link

Add line breaks into long lines #1

Closed blinry closed 2 years ago

blinry commented 2 years ago

When I tried using Whisper to generate subtitles for one of my own talks earlier today, one adjustment I needed to make is to break up long lines. For example, instead of

A friend of mine was of the opinion that it was missing an exclamation mark in the string.

it is preferable to have

A friend of mine was of the opinion
that it was missing an exclamation mark in the string.

Netflix, along with other guidelines, recommends:

Prefer a bottom-heavy pyramid shape for subtitles when multiple line break options present themselves, but avoid having just one or two words on the top line.

I think it would be really cool if we could also automate that process of breaking the text into lines! :) Cheers for your really cool work here! \o/

m1guelpf commented 2 years ago

Maybe we could find a library to generate the VTT instead of doing it manually? There has to be someone who's figured this out already

nogarcia commented 2 years ago

Although I couldn't find any existing Python implementation of this behavior, SubtitleEdit and Aegisub both have this feature.

There are several ways to do it (SubtitleEdit's process is probably the most robust,) but a simple method would be to count the characters in each segment returned from Whisper, and add a newline 42 characters or less from the end of the segment if necessary. Slicing from the end is important: the guidelines state that the second line should be the longest. If exactly 42 characters cuts off a word, the cut should be rounded to the end of that word.

In lieu of an automated method, manually passing through the subtitles with SubtitleEdit is a quick select-all in the meantime.

m1guelpf commented 2 years ago

@radiohazard-dev I'd accept a PR that adds this as an optional flag, if you're up for it :D