save_sub_helper.go 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package save_sub_helper
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/allanpk716/ChineseSubFinder/pkg"
  6. "github.com/allanpk716/ChineseSubFinder/pkg/change_file_encode"
  7. "github.com/allanpk716/ChineseSubFinder/pkg/chs_cht_changer"
  8. "github.com/allanpk716/ChineseSubFinder/pkg/ifaces"
  9. "github.com/allanpk716/ChineseSubFinder/pkg/logic/sub_timeline_fixer"
  10. "github.com/allanpk716/ChineseSubFinder/pkg/settings"
  11. "github.com/allanpk716/ChineseSubFinder/pkg/types/subparser"
  12. "github.com/sirupsen/logrus"
  13. )
  14. type SaveSubHelper struct {
  15. log *logrus.Logger
  16. settings *settings.Settings
  17. SubFormatter ifaces.ISubFormatter // 字幕格式化命名的实现
  18. subTimelineFixerHelperEx *sub_timeline_fixer.SubTimelineFixerHelperEx // 字幕时间轴校正
  19. }
  20. func NewSaveSubHelper(log *logrus.Logger, settings *settings.Settings, subFormatter ifaces.ISubFormatter, subTimelineFixerHelperEx *sub_timeline_fixer.SubTimelineFixerHelperEx) *SaveSubHelper {
  21. return &SaveSubHelper{settings: settings, log: log, SubFormatter: subFormatter, subTimelineFixerHelperEx: subTimelineFixerHelperEx}
  22. }
  23. // WriteSubFile2VideoPath 在前面需要进行语言的筛选、排序,这里仅仅是存储, extraSubPreName 这里传递是字幕的网站,有就认为是多字幕的存储。空就是单字幕,单字幕就可以setDefault
  24. func (s *SaveSubHelper) WriteSubFile2VideoPath(videoFileFullPath string, finalSubFile subparser.FileInfo, extraSubPreName string, setDefault bool, skipExistFile bool) error {
  25. defer s.log.Infoln("----------------------------------")
  26. videoRootPath := filepath.Dir(videoFileFullPath)
  27. subNewName, subNewNameWithDefault, _ := s.SubFormatter.GenerateMixSubName(videoFileFullPath, finalSubFile.Ext, finalSubFile.Lang, extraSubPreName)
  28. desSubFullPath := filepath.Join(videoRootPath, subNewName)
  29. if setDefault == true {
  30. // 先判断没有 default 的字幕是否存在了,在的话,先删除,然后再写入
  31. if pkg.IsFile(desSubFullPath) == true {
  32. _ = os.Remove(desSubFullPath)
  33. }
  34. desSubFullPath = filepath.Join(videoRootPath, subNewNameWithDefault)
  35. }
  36. if skipExistFile == true {
  37. // 需要判断文件是否存在在,有则跳过
  38. if pkg.IsFile(desSubFullPath) == true {
  39. s.log.Infoln("OrgSubName:", finalSubFile.Name)
  40. s.log.Infoln("Sub Skip DownAt:", desSubFullPath)
  41. return nil
  42. }
  43. }
  44. // 最后写入字幕
  45. err := pkg.WriteFile(desSubFullPath, finalSubFile.Data)
  46. if err != nil {
  47. return err
  48. }
  49. s.log.Infoln("----------------------------------")
  50. s.log.Infoln("OrgSubName:", finalSubFile.Name)
  51. s.log.Infoln("SubDownAt:", desSubFullPath)
  52. // 然后还需要判断是否需要校正字幕的时间轴
  53. if s.settings.AdvancedSettings.FixTimeLine == true {
  54. err = s.subTimelineFixerHelperEx.Process(videoFileFullPath, desSubFullPath)
  55. if err != nil {
  56. return err
  57. }
  58. }
  59. // 判断是否需要转换字幕的编码
  60. if s.settings.ExperimentalFunction.AutoChangeSubEncode.Enable == true {
  61. s.log.Infoln("----------------------------------")
  62. s.log.Infoln("change_file_encode to", s.settings.ExperimentalFunction.AutoChangeSubEncode.GetDesEncodeType())
  63. err = change_file_encode.Process(desSubFullPath, s.settings.ExperimentalFunction.AutoChangeSubEncode.DesEncodeType)
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. // 判断是否需要进行简繁互转
  69. // 一定得是 UTF-8 才能够执行简繁转换
  70. // 测试了先转 UTF-8 进行简繁转换然后再转 GBK,有些时候会出错,所以还是不支持这样先
  71. if s.settings.ExperimentalFunction.AutoChangeSubEncode.Enable == true &&
  72. s.settings.ExperimentalFunction.AutoChangeSubEncode.DesEncodeType == 0 &&
  73. s.settings.ExperimentalFunction.ChsChtChanger.Enable == true {
  74. s.log.Infoln("----------------------------------")
  75. s.log.Infoln("chs_cht_changer to", s.settings.ExperimentalFunction.ChsChtChanger.GetDesChineseLanguageTypeString())
  76. err = chs_cht_changer.Process(desSubFullPath, s.settings.ExperimentalFunction.ChsChtChanger.DesChineseLanguageType)
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. return nil
  82. }