ffmpeg_info.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package ffmpeg_helper
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/common"
  4. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
  8. "github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
  9. "path/filepath"
  10. "strings"
  11. )
  12. type FFMPEGInfo struct {
  13. VideoFullPath string // 视频文件的路径
  14. AudioInfoList []AudioInfo // 内置音频列表
  15. SubtitleInfoList []SubtitleInfo // 内置字幕列表
  16. ExternalSubInfos []*subparser.FileInfo // 外置字幕列表
  17. }
  18. func NewFFMPEGInfo(videoFullPath string) *FFMPEGInfo {
  19. return &FFMPEGInfo{
  20. VideoFullPath: videoFullPath,
  21. AudioInfoList: make([]AudioInfo, 0),
  22. SubtitleInfoList: make([]SubtitleInfo, 0),
  23. ExternalSubInfos: make([]*subparser.FileInfo, 0),
  24. }
  25. }
  26. // GetCacheFolderFPath 获取缓存文件夹的绝对路径,存储在通用的 SubFixCacheFolder 中
  27. // csf-cache/当前的视频文件名(不带后缀)
  28. func (f *FFMPEGInfo) GetCacheFolderFPath() (string, error) {
  29. noExtVideoName := strings.ReplaceAll(filepath.Base(f.VideoFullPath), filepath.Ext(f.VideoFullPath), "")
  30. return my_util.GetSubFixCacheFolderByName(noExtVideoName)
  31. }
  32. // IsExported 是否已经导出过,如果没有导出或者导出不完整为 false
  33. func (f *FFMPEGInfo) IsExported(exportType ExportType) bool {
  34. nowCacheFolder, err := f.GetCacheFolderFPath()
  35. if err != nil {
  36. log_helper.GetLogger().Errorln("FFMPEGInfo.IsExported.GetCacheFolderFPath", f.VideoFullPath, err.Error())
  37. return false
  38. }
  39. // 首先存储的缓存目录要存在
  40. if my_util.IsDir(nowCacheFolder) == false {
  41. return false
  42. }
  43. switch exportType {
  44. case Audio:
  45. // 音频是否导出了
  46. done := f.isAudioExported(nowCacheFolder)
  47. if done == false {
  48. return false
  49. }
  50. break
  51. case Subtitle:
  52. // 字幕都要导出了
  53. done := f.isSubExported(nowCacheFolder)
  54. if done == false {
  55. return false
  56. }
  57. case SubtitleAndAudio:
  58. // 音频是否导出了
  59. done := f.isAudioExported(nowCacheFolder)
  60. if done == false {
  61. return false
  62. }
  63. // 字幕都要导出了
  64. done = f.isSubExported(nowCacheFolder)
  65. if done == false {
  66. return false
  67. }
  68. default:
  69. return false
  70. }
  71. return true
  72. }
  73. func (f *FFMPEGInfo) isAudioExported(nowCacheFolder string) bool {
  74. for index, audioInfo := range f.AudioInfoList {
  75. audioFPath := filepath.Join(nowCacheFolder, audioInfo.GetName()+extPCM)
  76. if my_util.IsFile(audioFPath) == false {
  77. return false
  78. } else {
  79. f.AudioInfoList[index].FullPath = audioFPath
  80. }
  81. }
  82. return true
  83. }
  84. func (f *FFMPEGInfo) isSubExported(nowCacheFolder string) bool {
  85. for index, subtitleInfo := range f.SubtitleInfoList {
  86. subSrtFPath := filepath.Join(nowCacheFolder, subtitleInfo.GetName()+common.SubExtSRT)
  87. if my_util.IsFile(subSrtFPath) == false {
  88. return false
  89. } else {
  90. f.SubtitleInfoList[index].FullPath = subSrtFPath
  91. }
  92. subASSFPath := filepath.Join(nowCacheFolder, subtitleInfo.GetName()+common.SubExtASS)
  93. if my_util.IsFile(subASSFPath) == false {
  94. return false
  95. } else {
  96. f.SubtitleInfoList[index].FullPath = subASSFPath
  97. }
  98. }
  99. return true
  100. }
  101. // GetExternalSubInfos 获取外置的字幕信息
  102. func (f *FFMPEGInfo) GetExternalSubInfos(subParserHub *sub_parser_hub.SubParserHub) error {
  103. subFiles, err := sub_helper.SearchMatchedSubFileByOneVideo(f.VideoFullPath)
  104. if err != nil {
  105. return err
  106. }
  107. for _, subFile := range subFiles {
  108. bok, subInfo, err := subParserHub.DetermineFileTypeFromFile(subFile)
  109. if err != nil {
  110. return err
  111. }
  112. if bok == false {
  113. continue
  114. }
  115. f.ExternalSubInfos = append(f.ExternalSubInfos, subInfo)
  116. }
  117. return nil
  118. }