// Formats the subs while calculating subtitle offset shifts func formatSubtitles(language string, offset int, subtitles *TT) (string, error) { // Sets up default ASS sub info header := "[Script Info]\nTitle: Default Aegisub file\nScriptType: v4.00+\nWrapStyle: 0\nPlayResX: 656\nPlayResY: 368\n\n" styles := "[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n" events := "\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n" // Appends styles TODO (ignoring color) for _, style := range subtitles.Head.Styling.Styles { styles = styles + "Style: " + strconv.Itoa(style.ID) + "," + "Trebuchet MS" + "," + "24" + "," + "&H00FFFFFF" + "," + "&H000000FF" + "," + "&H00000000" + "," + "&H00000000" + "," + "0" + "," + "0" + "," + "0" + "," + "0" + "," + "100" + "," + "100" + "," + "0" + "," + "0" + "," + "1" + "," + "2" + "," + "0" + "," + "2" + "," + "0040" + "," + "0040" + "," + "0018" + "," + "0" + "\n" } // Appends all subtitle captions where the language matches what we want for _, subs := range subtitles.Body.Subtitles { if strings.ToLower(subs.Language) == strings.ToLower(language) { for _, event := range subs.Events { beginTime, _ := anirip.ShiftTime(event.Begin, offset) endTime, _ := anirip.ShiftTime(event.End, offset) events = events + "Dialogue: 0," + beginTime + "," + endTime + "," + strconv.Itoa(event.Style) + "," + "" + "," + // Name of the person doing the talking "0000" + "," + "0000" + "," + "0000" + "," + "" + "," + // Event fx strings.Replace(strings.TrimSpace(strings.Replace(event.Text, "\t", "", -1)), "\n", `\N`, -1) + "\n" // Removes all tabs, then removes leading/trailing whitespace, then replaces /n with /N for ASS formatting } } } // Returns the full subtitles as an ASS string representation return header + styles + events, nil }
func formatSubtitles(offset int, subString string) (string, error) { subScript := SubtitleScript{} // Parses the xml into our results object err := xml.Unmarshal([]byte(subString), &subScript) if err != nil { return "", anirip.Error{Message: "There was an error while parsing the XML subtitles", Err: err} } // Discarding language for now in order to set to default playback subtitle (subScript.Title) header := "[Script Info]\nTitle: Default Aegisub file\nScriptType: v4.00+\nWrapStyle: " + strconv.Itoa(subScript.WrapStyle) + "\nPlayResX: 656\nPlayResY: 368\n\n" styles := "[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n" events := "\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n" styleArray := subScript.Styles[0].Styles eventArray := subScript.Events[0].Events for _, style := range styleArray { styles = styles + "Style: " + style.Name + "," + style.FontName + "," + strconv.Itoa(style.FontSize) + "," + style.PrimaryColor + "," + style.SecondaryColor + "," + style.OutlineColor + "," + style.BackColor + "," + strconv.Itoa(style.Bold) + "," + strconv.Itoa(style.Italic) + "," + strconv.Itoa(style.Underline) + "," + strconv.Itoa(style.Strikeout) + "," + strconv.Itoa(style.ScaleX) + "," + strconv.Itoa(style.ScaleY) + "," + strconv.Itoa(style.Spacing) + "," + strconv.Itoa(style.Angle) + "," + strconv.Itoa(style.BorderStyle) + "," + strconv.Itoa(style.Outline) + "," + strconv.Itoa(style.Shadow) + "," + strconv.Itoa(style.Alignment) + "," + style.MarginLeft + "," + style.MarginRight + "," + style.MarginVert + "," + strconv.Itoa(style.Encoding) + "\n" } for _, event := range eventArray { beginTime, _ := anirip.ShiftTime(event.Start, offset) endTime, _ := anirip.ShiftTime(event.End, offset) events = events + "Dialogue: 0," + beginTime + "," + endTime + "," + event.Style + "," + event.Name + "," + event.MarginLeft + "," + event.MarginRight + "," + event.MarginVert + "," + event.Effect + "," + event.Text + "\n" } return header + styles + events, nil }