-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (108 loc) · 3.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/shipurjan/subtitle-to-lrc/converter"
"github.com/shipurjan/subtitle-to-lrc/converter/shared"
"github.com/shipurjan/subtitle-to-lrc/utils"
"github.com/urfave/cli/v2"
)
var (
version string
supported_extensions string
)
func main() {
app := &cli.App{
Name: "subtitle-to-lrc",
Version: version,
Usage: "Convert subtitle files to .lrc format",
UsageText: "subtitle-to-lrc [options] <input-file> [output-file]\n" +
"\n" +
"<input-file> - the file must have an allowed subtitle extension (" + supported_extensions + ")\n" +
"[output-file] - if not provided the program will use <input-file> filename with its extension replaced by .lrc",
Compiled: time.Now(),
EnableBashCompletion: true,
HideHelpCommand: true,
Authors: []*cli.Author{{Name: "Cyprian Zdebski", Email: "[email protected]"}},
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "separator",
Value: " ",
Aliases: []string{"s"},
Usage: "Separator to use to join lines when input subtitle file has multiple lines;\n" +
".lrc files can only have one subtitle line for each timestamp",
},
&cli.BoolFlag{
Name: "no-length-limit",
Aliases: []string{"n"},
Usage: "Disables the length limit of a .lrc file;\n" +
"by default a .lrc file can only have a maximum length of 59:59.99\n" +
"(some players may not support longer durations)",
},
},
Action: func(cCtx *cli.Context) error {
if cCtx.NArg() < 1 {
return errors.New("no input file provided, see --help for more information")
}
args := shared.UserArgs{
Separator: cCtx.String("separator"),
NoLengthLimit: cCtx.Bool("no-length-limit"),
}
// Read the argument
var input_path string
if len(cCtx.Args().Get(0)) == 0 {
return errors.New("no input file provided")
} else {
input_path = cCtx.Args().Get(0)
}
// Convert to absolute path
input_abs_path, err := filepath.Abs(input_path)
if err != nil {
return err
}
// Verify if the provided subtitle file is valid for conversion
subtitle_file, subtitle_extension, err := utils.ReadSubtitleFile(input_abs_path)
if err != nil {
return err
}
var output_path string
if len(cCtx.Args().Get(1)) == 0 {
output_path = strings.TrimSuffix(input_path, filepath.Ext(input_path)) + ".lrc"
} else {
output_path = cCtx.Args().Get(1)
}
output_abs_path, err := filepath.Abs(output_path)
if err != nil {
return err
}
// Convert subtitle file to lrc
lyrics_file, err := converter.ConvertToLyricsFile(subtitle_file, subtitle_extension, args)
if err != nil {
return err
}
if err := writeLyricsFile(lyrics_file, output_abs_path); err != nil {
return err
}
fmt.Println("[OK]", output_abs_path)
return nil
},
}
if err := app.Run(os.Args); err != nil {
fmt.Println("[ERROR]", err)
}
}
func writeLyricsFile(lyricsFile []string, outputFilename string) error {
// Join the lyrics lines into a single string
lyricsContent := strings.Join(lyricsFile, "\n")
// Write the lyrics content to the output file
err := os.WriteFile(outputFilename, []byte(lyricsContent), 0644)
if err != nil {
return err
}
return nil
}