feat: vtt converting
This commit is contained in:
parent
ba2e477dc0
commit
ba66894e42
7 changed files with 693 additions and 107 deletions
|
@ -152,3 +152,91 @@ func ConvertToLyrics(entries []model.SRTEntry) model.Lyrics {
|
|||
|
||||
return lyrics
|
||||
}
|
||||
|
||||
// ConvertToSubtitle converts SRT entries to our intermediate Subtitle structure
|
||||
func ConvertToSubtitle(filePath string) (model.Subtitle, error) {
|
||||
entries, err := Parse(filePath)
|
||||
if err != nil {
|
||||
return model.Subtitle{}, fmt.Errorf("error parsing SRT file: %w", err)
|
||||
}
|
||||
|
||||
subtitle := model.NewSubtitle()
|
||||
subtitle.Format = "srt"
|
||||
|
||||
// Convert SRT entries to intermediate representation
|
||||
for _, entry := range entries {
|
||||
subtitleEntry := model.NewSubtitleEntry()
|
||||
subtitleEntry.Index = entry.Number
|
||||
subtitleEntry.StartTime = entry.StartTime
|
||||
subtitleEntry.EndTime = entry.EndTime
|
||||
subtitleEntry.Text = entry.Content
|
||||
|
||||
// Look for HTML styling tags and store information about them
|
||||
if strings.Contains(entry.Content, "<") && strings.Contains(entry.Content, ">") {
|
||||
// Extract and store HTML styling info
|
||||
if strings.Contains(entry.Content, "<i>") || strings.Contains(entry.Content, "<I>") {
|
||||
subtitleEntry.Styles["italic"] = "true"
|
||||
}
|
||||
if strings.Contains(entry.Content, "<b>") || strings.Contains(entry.Content, "<B>") {
|
||||
subtitleEntry.Styles["bold"] = "true"
|
||||
}
|
||||
if strings.Contains(entry.Content, "<u>") || strings.Contains(entry.Content, "<U>") {
|
||||
subtitleEntry.Styles["underline"] = "true"
|
||||
}
|
||||
|
||||
subtitleEntry.FormatData["has_html_tags"] = true
|
||||
}
|
||||
|
||||
subtitle.Entries = append(subtitle.Entries, subtitleEntry)
|
||||
}
|
||||
|
||||
return subtitle, nil
|
||||
}
|
||||
|
||||
// ConvertFromSubtitle converts our intermediate Subtitle representation to SRT format
|
||||
func ConvertFromSubtitle(subtitle model.Subtitle, filePath string) error {
|
||||
var entries []model.SRTEntry
|
||||
|
||||
// Convert intermediate representation to SRT entries
|
||||
for i, subtitleEntry := range subtitle.Entries {
|
||||
entry := model.SRTEntry{
|
||||
Number: i + 1, // Ensure sequential numbering
|
||||
StartTime: subtitleEntry.StartTime,
|
||||
EndTime: subtitleEntry.EndTime,
|
||||
Content: subtitleEntry.Text,
|
||||
}
|
||||
|
||||
// Use index from original entry if available
|
||||
if subtitleEntry.Index > 0 {
|
||||
entry.Number = subtitleEntry.Index
|
||||
}
|
||||
|
||||
// Apply any styling stored in the entry if needed
|
||||
// Note: SRT only supports basic HTML tags, so we convert style attributes back to HTML
|
||||
content := entry.Content
|
||||
if _, ok := subtitleEntry.Styles["italic"]; ok && subtitleEntry.Styles["italic"] == "true" {
|
||||
if !strings.Contains(content, "<i>") {
|
||||
content = "<i>" + content + "</i>"
|
||||
}
|
||||
}
|
||||
if _, ok := subtitleEntry.Styles["bold"]; ok && subtitleEntry.Styles["bold"] == "true" {
|
||||
if !strings.Contains(content, "<b>") {
|
||||
content = "<b>" + content + "</b>"
|
||||
}
|
||||
}
|
||||
if _, ok := subtitleEntry.Styles["underline"]; ok && subtitleEntry.Styles["underline"] == "true" {
|
||||
if !strings.Contains(content, "<u>") {
|
||||
content = "<u>" + content + "</u>"
|
||||
}
|
||||
}
|
||||
|
||||
// Only update content if we applied styling
|
||||
if content != entry.Content {
|
||||
entry.Content = content
|
||||
}
|
||||
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
|
||||
return Generate(entries, filePath)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue