Open darodi opened 2 years ago
Hey, just took a look at the webvtt-py
docs and no, this is currently not possible. It really would be a great feature. The most it can do is spit out text as is, that is:
1
00:00:07,120 --> 00:00:09,480
<c.magenta.bg_black>Musique douce</c>
2
00:00:09,720 --> 00:00:29,520
<c.magenta.bg_black>---</c>
3
00:00:32,439 --> 00:00:35,320
<c.magenta.bg_black>Musique douce</c>
4
00:00:35,560 --> 00:02:25,240
<c.magenta.bg_black>---</c>
5
00:02:25,480 --> 00:02:27,440
<c.white.bg_black>-Stéphane ? Où on se gare ?</c>
You made me realize that this might be desirable if the style was in-line, so I'm going to add a flag that would allow that (keep caption text as is).
Looking at the code I realized the dummy here was importing an unused library (html2text
). Pushed a commit removing that requirement.
Thanks for your feedback.
When you talked about webvtt-py
, I had a look at your code and improved the script to store each colour style and change it in each caption in the loop.
I might create a pull request, but I'm not so happy about it.
One better solution would be to modify the webvtt-py
library directly to be able to manage font styles.
As said here: https://webvtt-py.readthedocs.io/en/latest/usage.html#converting-captions These few lines of code would just be enough.
import webvtt
# save in SRT format
vtt = webvtt.read('captions.vtt')
vtt.save_as_srt()
I'll check their code and create a pull request on their project.
By the way, why did you loop on each caption instead of what is preconized by webvtt-py
? Was there a reason?
I might have a look at ffmpeg
's way of converting subtitles in their code too.
I tried
ffmpeg.exe -i captions.vtt captions.srt
Usually, it works, but with my input file containing styles, I get an empty output.
... As said here: https://webvtt-py.readthedocs.io/en/latest/usage.html#converting-captions These few lines of code would just be enough.
import webvtt # save in SRT format vtt = webvtt.read('captions.vtt') vtt.save_as_srt()
Because save_as_srt()
still uses webvtt.Caption.raw_text
. Tested it last Saturday and just now. The video player I used at the time on Windows machines then did not support WebVTT, nor did it parse any tags on SRT files. Moreover, even webvtt.Caption.text
sometimes still had tags in it (at least 4 years ago), hence html.unescape
.
I've also created a pull request for webvtt-py
Following your remark here: posted by @lbrayner in https://github.com/glut23/webvtt-py/issues/39#issuecomment-1021560071 For your information, pull request #3 was already html unescaped, no problem here.
def replace_color(x, tag_name, v):
return ("" if tag_name == "c" else ("<" + tag_name + ">")) \
+ "<font color=\"" + v + "\">" \
+ html.unescape(x.group(1)) \
+ "</font>" \
+ ("" if tag_name == "c" else ("</" + tag_name + ">"))
if no_tag_found:
caption_text = html.unescape(caption.text)
First of all, thanks for your script. It was the only converter I found on github actually working when there are styles in the input vtt.
but would it be possible to add font style in the output? :) For example here is an input:
the current output is:
The desired output would be