adding subtitle flag for archive (-s, --sublang)

This commit is contained in:
LordBex 2022-09-05 15:40:49 +02:00
parent 027047fc7e
commit 97dd801137

View file

@ -5,12 +5,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"github.com/crunchy-labs/crunchy-cli/cli/commands"
"github.com/crunchy-labs/crunchy-cli/utils"
"github.com/crunchy-labs/crunchyroll-go/v3"
crunchyUtils "github.com/crunchy-labs/crunchyroll-go/v3/utils"
"github.com/grafov/m3u8"
"github.com/spf13/cobra"
"io" "io"
"math" "math"
"os" "os"
@ -22,10 +16,18 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"github.com/crunchy-labs/crunchy-cli/cli/commands"
"github.com/crunchy-labs/crunchy-cli/utils"
"github.com/crunchy-labs/crunchyroll-go/v3"
crunchyUtils "github.com/crunchy-labs/crunchyroll-go/v3/utils"
"github.com/grafov/m3u8"
"github.com/spf13/cobra"
) )
var ( var (
archiveLanguagesFlag []string archiveLanguagesFlag []string
archiveSubLanguagesFlag []string
archiveDirectoryFlag string archiveDirectoryFlag string
archiveOutputFlag string archiveOutputFlag string
@ -69,6 +71,18 @@ var Cmd = &cobra.Command{
} }
utils.Log.Debug("Using following audio locales: %s", strings.Join(archiveLanguagesFlag, ", ")) utils.Log.Debug("Using following audio locales: %s", strings.Join(archiveLanguagesFlag, ", "))
for _, locale := range archiveSubLanguagesFlag {
if !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(locale)) {
// if locale is 'all', match all known locales
if locale == "all" {
archiveSubLanguagesFlag = utils.LocalesAsStrings()
break
}
return fmt.Errorf("%s is not a valid locale for Subtitels. Choose from: %s", locale, strings.Join(utils.LocalesAsStrings(), ", "))
}
}
utils.Log.Debug("Using following subtitels locales: %s", strings.Join(archiveSubLanguagesFlag, ", "))
var found bool var found bool
for _, mode := range []string{"auto", "audio", "video"} { for _, mode := range []string{"auto", "audio", "video"} {
if archiveMergeFlag == mode { if archiveMergeFlag == mode {
@ -127,12 +141,20 @@ func init() {
[]string{string(utils.SystemLocale(false)), string(crunchyroll.JP)}, []string{string(utils.SystemLocale(false)), string(crunchyroll.JP)},
"Audio locale which should be downloaded. Can be used multiple times") "Audio locale which should be downloaded. Can be used multiple times")
Cmd.Flags().StringSliceVarP(&archiveSubLanguagesFlag,
"sublang",
"s",
[]string{},
"Subtitles langs which should be downloaded. Can be used multiple times")
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
Cmd.Flags().StringVarP(&archiveDirectoryFlag, Cmd.Flags().StringVarP(&archiveDirectoryFlag,
"directory", "directory",
"d", "d",
cwd, cwd,
"The directory to store the files into") "The directory to store the files into")
Cmd.Flags().StringVarP(&archiveOutputFlag, Cmd.Flags().StringVarP(&archiveOutputFlag,
"output", "output",
"o", "o",
@ -147,6 +169,7 @@ func init() {
"\t{fps} » Frame Rate of the video\n"+ "\t{fps} » Frame Rate of the video\n"+
"\t{audio} » Audio locale of the video\n"+ "\t{audio} » Audio locale of the video\n"+
"\t{subtitle} » Subtitle locale of the video") "\t{subtitle} » Subtitle locale of the video")
Cmd.Flags().StringVar(&archiveTempDirFlag, Cmd.Flags().StringVar(&archiveTempDirFlag,
"temp", "temp",
os.TempDir(), os.TempDir(),
@ -507,10 +530,25 @@ func archiveDownloadVideos(downloader crunchyroll.Downloader, filename string, v
return files, nil return files, nil
} }
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func archiveDownloadSubtitles(filename string, subtitles ...*crunchyroll.Subtitle) ([]string, error) { func archiveDownloadSubtitles(filename string, subtitles ...*crunchyroll.Subtitle) ([]string, error) {
var files []string var files []string
for _, subtitle := range subtitles { for _, subtitle := range subtitles {
if len(archiveSubLanguagesFlag) > 0 {
if !stringInSlice(string(subtitle.Locale), archiveSubLanguagesFlag) {
continue
}
}
f, err := os.CreateTemp("", fmt.Sprintf("%s_%s_subtitle_*.ass", filename, subtitle.Locale)) f, err := os.CreateTemp("", fmt.Sprintf("%s_%s_subtitle_*.ass", filename, subtitle.Locale))
if err != nil { if err != nil {
return nil, err return nil, err