64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package sync
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"sub-cli/internal/format/lrc"
|
|
"sub-cli/internal/model"
|
|
)
|
|
|
|
// syncLRCFiles synchronizes two LRC files
|
|
func syncLRCFiles(sourceFile, targetFile string) error {
|
|
source, err := lrc.Parse(sourceFile)
|
|
if err != nil {
|
|
return fmt.Errorf("error parsing source file: %w", err)
|
|
}
|
|
|
|
target, err := lrc.Parse(targetFile)
|
|
if err != nil {
|
|
return fmt.Errorf("error parsing target file: %w", err)
|
|
}
|
|
|
|
// Check if line counts match
|
|
if len(source.Timeline) != len(target.Content) {
|
|
fmt.Printf("Warning: Source timeline (%d entries) and target content (%d entries) have different counts. Timeline will be scaled.\n",
|
|
len(source.Timeline), len(target.Content))
|
|
}
|
|
|
|
// Apply timeline from source to target
|
|
syncedLyrics := syncLRCTimeline(source, target)
|
|
|
|
// Write the synced lyrics to the target file
|
|
return lrc.Generate(syncedLyrics, targetFile)
|
|
}
|
|
|
|
// syncLRCTimeline applies the timeline from the source lyrics to the target lyrics
|
|
func syncLRCTimeline(source, target model.Lyrics) model.Lyrics {
|
|
result := model.Lyrics{
|
|
Metadata: target.Metadata,
|
|
Content: target.Content,
|
|
}
|
|
|
|
// If target has no content, return empty result with metadata only
|
|
if len(target.Content) == 0 {
|
|
result.Timeline = []model.Timestamp{}
|
|
return result
|
|
}
|
|
|
|
// If source has no timeline, keep target as is
|
|
if len(source.Timeline) == 0 {
|
|
result.Timeline = target.Timeline
|
|
return result
|
|
}
|
|
|
|
// Scale the source timeline to match the target content length
|
|
if len(source.Timeline) != len(target.Content) {
|
|
result.Timeline = scaleTimeline(source.Timeline, len(target.Content))
|
|
} else {
|
|
// If lengths match, directly use source timeline
|
|
result.Timeline = make([]model.Timestamp, len(source.Timeline))
|
|
copy(result.Timeline, source.Timeline)
|
|
}
|
|
|
|
return result
|
|
}
|