@@ -46,7 +46,6 @@ var chapterSplitCmd = &cobra.Command{
4646}
4747
4848func chapterSplit (cmd * cobra.Command , args []string ) {
49-
5049 _ , err := exec .LookPath ("ffprobe" )
5150
5251 if err != nil {
@@ -67,6 +66,10 @@ func chapterSplit(cmd *cobra.Command, args []string) {
6766 }
6867
6968 data , err := getChapters (target )
69+ if err != nil {
70+ log .Fatal ().Err (err ).Msg ("Could not extract chapters" )
71+ }
72+
7073 base := strings .Trim (path .Base (target ), path .Ext (target ))
7174 targetdir := fmt .Sprintf ("split_%s" , base )
7275 err = os .MkdirAll (targetdir , 0777 )
@@ -79,18 +82,19 @@ func chapterSplit(cmd *cobra.Command, args []string) {
7982
8083 for _ , c := range data .Chapters {
8184 wg .Add (1 )
85+
8286 go copyChapter (& wg , c , target , targetdir )
8387 }
8488
8589 wg .Wait ()
8690}
8791
88- func copyChapter (wg * sync.WaitGroup , c chapter , sourcefile , targetdir string ) error {
92+ func copyChapter (wg * sync.WaitGroup , c chapter , sourcefile , targetdir string ) {
8993 defer wg .Done ()
9094
9195 title := strings .Trim (c .Tags .Title , " \n \r " )
9296 safetitle := sanitize .Name (title )
93- prefix := fmt .Sprintf ("%03d_" , c .Id )
97+ prefix := fmt .Sprintf ("%03d_" , c .ID )
9498 outfile := filepath .Join (targetdir , prefix + safetitle + path .Ext (sourcefile ))
9599
96100 cmd := exec .Command ("ffmpeg" ,
@@ -107,8 +111,6 @@ func copyChapter(wg *sync.WaitGroup, c chapter, sourcefile, targetdir string) er
107111 if err != nil {
108112 log .Error ().Err (err ).Msgf ("%s: %s\n " , outfile , output )
109113 }
110-
111- return err
112114}
113115
114116// sample json output
@@ -135,7 +137,7 @@ type tags struct {
135137type chapter struct {
136138 StartTime string `json:"start_time"`
137139 EndTime string `json:"end_time"`
138- Id int `json:"id"`
140+ ID int `json:"id"`
139141 Tags tags `json:"tags"`
140142}
141143
@@ -154,16 +156,19 @@ func getChapters(target string) (ffmprobeResponse, error) {
154156 response := ffmprobeResponse {}
155157
156158 if err != nil {
157- return response , err
159+ return response , fmt . Errorf ( "could not probe chapters: %w" , err )
158160 }
159161
160162 err = json .Unmarshal (output , & response )
161163
162- return response , err
164+ if err != nil {
165+ return response , fmt .Errorf ("failed unmarshaling ffprobe response: %w" , err )
166+ }
167+
168+ return response , nil
163169}
164170
165171func chapterList (cmd * cobra.Command , args []string ) {
166-
167172 _ , err := exec .LookPath ("ffprobe" )
168173
169174 if err != nil {
@@ -191,7 +196,7 @@ func chapterList(cmd *cobra.Command, args []string) {
191196
192197 formattedJSON , err := json .MarshalIndent (data , "" , " " )
193198 if err != nil {
194- log .Fatal ().Err (err ).Msg ("Trouble marshalling to JSON" )
199+ log .Fatal ().Err (err ).Msg ("Trouble marshaling to JSON" )
195200 }
196201
197202 _ , _ = os .Stdout .Write (formattedJSON )
0 commit comments