Compare commits
No commits in common. "main" and "v0.3.0" have entirely different histories.
3 changed files with 11 additions and 51 deletions
|
@ -1,6 +1,6 @@
|
||||||
# lrc-cli
|
# lrc-cli
|
||||||
|
|
||||||
A CLI tool for LRC.
|
[WIP] A CLI tool for LRC.
|
||||||
See [releases](https://git.owu.one/starset-mirror/lrc-cli/releases) for binaries.
|
See [releases](https://git.owu.one/starset-mirror/lrc-cli/releases) for binaries.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
|
@ -90,12 +90,6 @@ func srtToLrc(sourceFile, targetFile string) {
|
||||||
Content: make([]string, len(srtEntries)),
|
Content: make([]string, len(srtEntries)),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add default metadata
|
|
||||||
title := strings.TrimSuffix(filepath.Base(targetFile), filepath.Ext(targetFile))
|
|
||||||
lyrics.Metadata["ti"] = title
|
|
||||||
lyrics.Metadata["ar"] = ""
|
|
||||||
lyrics.Metadata["al"] = ""
|
|
||||||
|
|
||||||
for i, entry := range srtEntries {
|
for i, entry := range srtEntries {
|
||||||
lyrics.Timeline[i] = entry.StartTime
|
lyrics.Timeline[i] = entry.StartTime
|
||||||
lyrics.Content[i] = entry.Content
|
lyrics.Content[i] = entry.Content
|
||||||
|
|
54
srt.go
54
srt.go
|
@ -19,55 +19,35 @@ func parseSRT(filePath string) ([]SRTEntry, error) {
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
var entries []SRTEntry
|
var entries []SRTEntry
|
||||||
var currentEntry SRTEntry
|
var currentEntry SRTEntry
|
||||||
var isContent bool
|
|
||||||
var contentBuffer strings.Builder
|
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
|
||||||
if line == "" {
|
if line == "" {
|
||||||
if currentEntry.Number != 0 {
|
if currentEntry.Number != 0 {
|
||||||
currentEntry.Content = contentBuffer.String()
|
|
||||||
entries = append(entries, currentEntry)
|
entries = append(entries, currentEntry)
|
||||||
currentEntry = SRTEntry{}
|
currentEntry = SRTEntry{}
|
||||||
isContent = false
|
|
||||||
contentBuffer.Reset()
|
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if currentEntry.Number == 0 {
|
if currentEntry.Number == 0 {
|
||||||
currentEntry.Number, _ = strconv.Atoi(line)
|
currentEntry.Number, _ = strconv.Atoi(line)
|
||||||
} else if isEntryTimeStampUnset(currentEntry) {
|
} else if currentEntry.StartTime.Hours == 0 {
|
||||||
times := strings.Split(line, " --> ")
|
times := strings.Split(line, " --> ")
|
||||||
if len(times) == 2 {
|
currentEntry.StartTime = parseSRTTimestamp(times[0])
|
||||||
currentEntry.StartTime = parseSRTTimestamp(times[0])
|
currentEntry.EndTime = parseSRTTimestamp(times[1])
|
||||||
currentEntry.EndTime = parseSRTTimestamp(times[1])
|
} else {
|
||||||
isContent = true
|
currentEntry.Content += line + "\n"
|
||||||
}
|
|
||||||
} else if isContent {
|
|
||||||
if contentBuffer.Len() > 0 {
|
|
||||||
contentBuffer.WriteString("\n")
|
|
||||||
}
|
|
||||||
contentBuffer.WriteString(line)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if currentEntry.Number != 0 {
|
if currentEntry.Number != 0 {
|
||||||
currentEntry.Content = contentBuffer.String()
|
|
||||||
entries = append(entries, currentEntry)
|
entries = append(entries, currentEntry)
|
||||||
}
|
}
|
||||||
|
|
||||||
return entries, scanner.Err()
|
return entries, scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func isEntryTimeStampUnset(currentEntry SRTEntry) bool {
|
|
||||||
return currentEntry.StartTime.Hours == 0 && currentEntry.StartTime.Minutes == 0 &&
|
|
||||||
currentEntry.StartTime.Seconds == 0 && currentEntry.StartTime.Milliseconds == 0 &&
|
|
||||||
currentEntry.EndTime.Hours == 0 && currentEntry.EndTime.Minutes == 0 &&
|
|
||||||
currentEntry.EndTime.Seconds == 0 && currentEntry.EndTime.Milliseconds == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertSRT(sourceFile, targetFile, targetFmt string) {
|
func convertSRT(sourceFile, targetFile, targetFmt string) {
|
||||||
switch targetFmt {
|
switch targetFmt {
|
||||||
case "txt":
|
case "txt":
|
||||||
|
@ -84,26 +64,12 @@ func formatSRTTimestamp(ts Timestamp) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSRTTimestamp(timeStr string) Timestamp {
|
func parseSRTTimestamp(timeStr string) Timestamp {
|
||||||
parts := strings.Split(timeStr, ",")
|
t, _ := time.Parse("15:04:05,000", timeStr)
|
||||||
if len(parts) != 2 {
|
|
||||||
return Timestamp{}
|
|
||||||
}
|
|
||||||
|
|
||||||
timeParts := strings.Split(parts[0], ":")
|
|
||||||
if len(timeParts) != 3 {
|
|
||||||
return Timestamp{}
|
|
||||||
}
|
|
||||||
|
|
||||||
hours, _ := strconv.Atoi(timeParts[0])
|
|
||||||
minutes, _ := strconv.Atoi(timeParts[1])
|
|
||||||
seconds, _ := strconv.Atoi(timeParts[2])
|
|
||||||
milliseconds, _ := strconv.Atoi(parts[1])
|
|
||||||
|
|
||||||
return Timestamp{
|
return Timestamp{
|
||||||
Hours: hours,
|
Hours: t.Hour(),
|
||||||
Minutes: minutes,
|
Minutes: t.Minute(),
|
||||||
Seconds: seconds,
|
Seconds: t.Second(),
|
||||||
Milliseconds: milliseconds,
|
Milliseconds: t.Nanosecond() / 1e6,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue