util.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. package my_util
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "crypto/sha256"
  7. "encoding/binary"
  8. "encoding/hex"
  9. "fmt"
  10. "github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
  11. "github.com/allanpk716/ChineseSubFinder/internal/pkg/regex_things"
  12. "github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
  13. "github.com/allanpk716/ChineseSubFinder/internal/types/common"
  14. browser "github.com/allanpk716/fake-useragent"
  15. "github.com/go-resty/resty/v2"
  16. "github.com/google/uuid"
  17. "github.com/sirupsen/logrus"
  18. "io"
  19. "math"
  20. "net/http"
  21. "net/url"
  22. "os"
  23. "os/exec"
  24. "path"
  25. "path/filepath"
  26. "regexp"
  27. "runtime"
  28. "strconv"
  29. "strings"
  30. "time"
  31. )
  32. // NewHttpClient 新建一个 resty 的对象
  33. func NewHttpClient(_proxySettings ...*settings.ProxySettings) *resty.Client {
  34. //const defUserAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"
  35. //const defUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41"
  36. var proxySettings *settings.ProxySettings
  37. var HttpProxy, UserAgent, Referer string
  38. if len(_proxySettings) > 0 {
  39. proxySettings = _proxySettings[0]
  40. if proxySettings.UseHttpProxy == true && len(proxySettings.HttpProxyAddress) > 0 {
  41. HttpProxy = proxySettings.HttpProxyAddress
  42. }
  43. }
  44. // 随机的 Browser
  45. UserAgent = browser.Random()
  46. httpClient := resty.New()
  47. httpClient.SetTimeout(common.HTMLTimeOut)
  48. httpClient.SetRetryCount(2)
  49. if HttpProxy != "" {
  50. httpClient.SetProxy(HttpProxy)
  51. if len(proxySettings.Referer) > 0 {
  52. Referer = proxySettings.Referer
  53. }
  54. if len(Referer) > 0 {
  55. httpClient.SetHeader("Referer", Referer)
  56. }
  57. } else {
  58. httpClient.RemoveProxy()
  59. }
  60. httpClient.SetHeaders(map[string]string{
  61. "Content-Type": "application/json",
  62. "User-Agent": UserAgent,
  63. })
  64. return httpClient
  65. }
  66. func GetPublicIP(queue *settings.TaskQueue, _proxySettings ...*settings.ProxySettings) string {
  67. var client *resty.Client
  68. if len(_proxySettings) > 0 {
  69. client = NewHttpClient(_proxySettings[0])
  70. } else {
  71. client = NewHttpClient()
  72. }
  73. targetSite := "http://myexternalip.com/raw"
  74. if queue.CheckPublicIPTargetSite != "" {
  75. targetSite = queue.CheckPublicIPTargetSite
  76. }
  77. response, err := client.R().Get(targetSite)
  78. if err != nil {
  79. return ""
  80. }
  81. return response.String()
  82. }
  83. // DownFile 从指定的 url 下载文件
  84. func DownFile(l *logrus.Logger, urlStr string, _proxySettings ...*settings.ProxySettings) ([]byte, string, error) {
  85. var proxySettings *settings.ProxySettings
  86. if len(_proxySettings) > 0 {
  87. proxySettings = _proxySettings[0]
  88. }
  89. var httpClient *resty.Client
  90. if proxySettings != nil {
  91. httpClient = NewHttpClient(proxySettings)
  92. } else {
  93. httpClient = NewHttpClient()
  94. }
  95. resp, err := httpClient.R().Get(urlStr)
  96. if err != nil {
  97. return nil, "", err
  98. }
  99. filename := GetFileName(l, resp.RawResponse)
  100. if filename == "" {
  101. l.Warningln("DownFile.GetFileName is string.empty", urlStr)
  102. }
  103. return resp.Body(), filename, nil
  104. }
  105. // GetFileName 获取下载文件的文件名
  106. func GetFileName(l *logrus.Logger, resp *http.Response) string {
  107. contentDisposition := resp.Header.Get("Content-Disposition")
  108. if len(contentDisposition) == 0 {
  109. m := regexp.MustCompile(`^(.*/)?(?:$|(.+?)(?:(\.[^.]*$)|$))`).FindStringSubmatch(resp.Request.URL.String())
  110. if m == nil || len(m) < 4 {
  111. l.Warningln("GetFileName.regexp.MustCompile.FindStringSubmatch", resp.Request.URL.String())
  112. return ""
  113. }
  114. return m[2] + m[3]
  115. }
  116. re := regexp.MustCompile(`filename=["]*([^"]+)["]*`)
  117. matched := re.FindStringSubmatch(contentDisposition)
  118. if matched == nil || len(matched) == 0 || len(matched[0]) == 0 {
  119. l.Errorln("GetFileName.Content-Disposition", contentDisposition)
  120. return ""
  121. }
  122. return matched[1]
  123. }
  124. // AddBaseUrl 判断传入的 url 是否需要拼接 baseUrl
  125. func AddBaseUrl(baseUrl, url string) string {
  126. if strings.Contains(url, "://") {
  127. return url
  128. }
  129. return fmt.Sprintf("%s%s", baseUrl, url)
  130. }
  131. // IsDir 存在且是文件夹
  132. func IsDir(path string) bool {
  133. s, err := os.Stat(path)
  134. if err != nil {
  135. return false
  136. }
  137. return s.IsDir()
  138. }
  139. // IsFile 存在且是文件
  140. func IsFile(filePath string) bool {
  141. s, err := os.Stat(filePath)
  142. if err != nil {
  143. return false
  144. }
  145. return !s.IsDir()
  146. }
  147. // VideoNameSearchKeywordMaker 拼接视频搜索的 title 和 年份
  148. func VideoNameSearchKeywordMaker(l *logrus.Logger, title string, year string) string {
  149. iYear, err := strconv.Atoi(year)
  150. if err != nil {
  151. // 允许的错误
  152. l.Errorln("VideoNameSearchKeywordMaker", "year to int", err)
  153. iYear = 0
  154. }
  155. searchKeyword := title
  156. if iYear >= 2020 {
  157. searchKeyword = searchKeyword + " " + year
  158. }
  159. return searchKeyword
  160. }
  161. // SearchMatchedVideoFileFromDirs 搜索符合后缀名的视频文件
  162. func SearchMatchedVideoFileFromDirs(l *logrus.Logger, dirs []string) ([]string, error) {
  163. defer func() {
  164. l.Debugln("SearchMatchedVideoFileFromDirs End ----------------")
  165. }()
  166. l.Debugln("SearchMatchedVideoFileFromDirs Start ----------------")
  167. var fileFullPathList = make([]string, 0)
  168. for _, dir := range dirs {
  169. matchedVideoFile, err := SearchMatchedVideoFile(l, dir)
  170. if err != nil {
  171. return nil, err
  172. }
  173. fileFullPathList = append(fileFullPathList, matchedVideoFile...)
  174. }
  175. for _, s := range fileFullPathList {
  176. l.Debugln(s)
  177. }
  178. return fileFullPathList, nil
  179. }
  180. // SearchMatchedVideoFile 搜索符合后缀名的视频文件,现在也会把 BDMV 的文件搜索出来,但是这个并不是一个视频文件,需要在后续特殊处理
  181. func SearchMatchedVideoFile(l *logrus.Logger, dir string) ([]string, error) {
  182. var fileFullPathList = make([]string, 0)
  183. pathSep := string(os.PathSeparator)
  184. files, err := os.ReadDir(dir)
  185. if err != nil {
  186. return nil, err
  187. }
  188. for _, curFile := range files {
  189. fullPath := dir + pathSep + curFile.Name()
  190. if curFile.IsDir() {
  191. // 内层的错误就无视了
  192. oneList, _ := SearchMatchedVideoFile(l, fullPath)
  193. if oneList != nil {
  194. fileFullPathList = append(fileFullPathList, oneList...)
  195. }
  196. } else {
  197. // 这里就是文件了
  198. bok, fakeBDMVVideoFile := FileNameIsBDMV(fullPath)
  199. if bok == true {
  200. // 这类文件后续的扫描字幕操作需要额外的处理
  201. fileFullPathList = append(fileFullPathList, fakeBDMVVideoFile)
  202. continue
  203. }
  204. if IsWantedVideoExtDef(curFile.Name()) == false {
  205. // 不是期望的视频后缀名则跳过
  206. continue
  207. } else {
  208. // 这里还有一种情况,就是蓝光, BDMV 下面会有一个 STREAM 文件夹,里面很多 m2ts 的视频组成
  209. if filepath.Base(filepath.Dir(fullPath)) == "STREAM" {
  210. l.Debugln("SearchMatchedVideoFile, Skip BDMV.STREAM:", fullPath)
  211. continue
  212. }
  213. // 跳过不符合的文件,比如 MAC OS 下可能有缓存文件,见 #138
  214. fi, err := curFile.Info()
  215. if err != nil {
  216. l.Debugln("SearchMatchedVideoFile, file.Info:", fullPath, err)
  217. continue
  218. }
  219. if fi.Size() == 4096 && strings.HasPrefix(curFile.Name(), "._") == true {
  220. l.Debugln("SearchMatchedVideoFile file.Size() == 4096 && Prefix Name == ._*", fullPath)
  221. continue
  222. }
  223. fileFullPathList = append(fileFullPathList, fullPath)
  224. }
  225. }
  226. }
  227. return fileFullPathList, nil
  228. }
  229. // FileNameIsBDMV 是否是 BDMV 蓝光目录,符合返回 true,以及 fakseVideoFPath
  230. func FileNameIsBDMV(id_bdmv_fileFPath string) (bool, string) {
  231. /*
  232. 这类蓝光视频比较特殊,它没有具体的一个后缀名的视频文件而是由两个文件夹来存储视频数据
  233. * BDMV
  234. * CERTIFICATE
  235. 但是不管如何,都需要使用一个文件作为锚点,就选定 CERTIFICATE 中的 id.bdmv 文件
  236. 后续的下载逻辑也需要单独为这个文件进行处理,比如,从这个文件向上一层获取 nfo 文件,
  237. 以及再上一层得到视频文件夹名称等
  238. */
  239. if strings.ToLower(filepath.Base(id_bdmv_fileFPath)) == common.FileBDMV {
  240. // 这个文件是确认了,那么就需要查看这个文件父级目录是不是 CERTIFICATE 文件夹
  241. // 且 CERTIFICATE 需要和 BDMV 文件夹都存在
  242. CERDir := filepath.Dir(id_bdmv_fileFPath)
  243. BDMVDir := filepath.Join(filepath.Dir(CERDir), "BDMV")
  244. if IsDir(CERDir) == true && IsDir(BDMVDir) == true {
  245. return true, filepath.Join(filepath.Dir(CERDir), filepath.Base(filepath.Dir(CERDir))+common.VideoExtMp4)
  246. }
  247. }
  248. return false, ""
  249. }
  250. func SearchTVNfo(l *logrus.Logger, dir string) ([]string, error) {
  251. var fileFullPathList = make([]string, 0)
  252. pathSep := string(os.PathSeparator)
  253. files, err := os.ReadDir(dir)
  254. if err != nil {
  255. return nil, err
  256. }
  257. for _, curFile := range files {
  258. fullPath := dir + pathSep + curFile.Name()
  259. if curFile.IsDir() {
  260. // 内层的错误就无视了
  261. oneList, _ := SearchTVNfo(l, fullPath)
  262. if oneList != nil {
  263. fileFullPathList = append(fileFullPathList, oneList...)
  264. }
  265. } else {
  266. // 这里就是文件了
  267. if strings.ToLower(curFile.Name()) != decode.MetadateTVNfo {
  268. continue
  269. } else {
  270. // 跳过不符合的文件,比如 MAC OS 下可能有缓存文件,见 #138
  271. fi, err := curFile.Info()
  272. if err != nil {
  273. l.Debugln("SearchTVNfo, file.Info:", fullPath, err)
  274. continue
  275. }
  276. if fi.Size() == 4096 && strings.HasPrefix(curFile.Name(), "._") == true {
  277. l.Debugln("SearchTVNfo file.Size() == 4096 && Prefix Name == ._*", fullPath)
  278. continue
  279. }
  280. fileFullPathList = append(fileFullPathList, fullPath)
  281. }
  282. }
  283. }
  284. return fileFullPathList, nil
  285. }
  286. // IsWantedVideoExtDef 后缀名是否符合规则
  287. func IsWantedVideoExtDef(fileName string) bool {
  288. if len(_wantedExtMap) < 1 {
  289. _defExtMap[common.VideoExtMp4] = common.VideoExtMp4
  290. _defExtMap[common.VideoExtMkv] = common.VideoExtMkv
  291. _defExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
  292. _defExtMap[common.VideoExtIso] = common.VideoExtIso
  293. _defExtMap[common.VideoExtM2ts] = common.VideoExtM2ts
  294. _wantedExtMap[common.VideoExtMp4] = common.VideoExtMp4
  295. _wantedExtMap[common.VideoExtMkv] = common.VideoExtMkv
  296. _wantedExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
  297. _wantedExtMap[common.VideoExtIso] = common.VideoExtIso
  298. _wantedExtMap[common.VideoExtM2ts] = common.VideoExtM2ts
  299. for _, videoExt := range _customVideoExts {
  300. _wantedExtMap[videoExt] = videoExt
  301. }
  302. }
  303. fileExt := strings.ToLower(filepath.Ext(fileName))
  304. _, bFound := _wantedExtMap[fileExt]
  305. return bFound
  306. }
  307. func GetEpisodeKeyName(season, eps int) string {
  308. return "S" + strconv.Itoa(season) + "E" + strconv.Itoa(eps)
  309. }
  310. // CopyFile copies a single file from src to dst
  311. func CopyFile(src, dst string) error {
  312. var err error
  313. var srcFd *os.File
  314. var dstFd *os.File
  315. var srcInfo os.FileInfo
  316. if srcFd, err = os.Open(src); err != nil {
  317. return err
  318. }
  319. defer func() {
  320. _ = srcFd.Close()
  321. }()
  322. if dstFd, err = os.Create(dst); err != nil {
  323. return err
  324. }
  325. defer func() {
  326. _ = dstFd.Close()
  327. }()
  328. if _, err = io.Copy(dstFd, srcFd); err != nil {
  329. return err
  330. }
  331. if srcInfo, err = os.Stat(src); err != nil {
  332. return err
  333. }
  334. return os.Chmod(dst, srcInfo.Mode())
  335. }
  336. // CopyDir copies a whole directory recursively
  337. func CopyDir(src string, dst string) error {
  338. var err error
  339. var fds []os.DirEntry
  340. var srcInfo os.FileInfo
  341. if srcInfo, err = os.Stat(src); err != nil {
  342. return err
  343. }
  344. if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
  345. return err
  346. }
  347. if fds, err = os.ReadDir(src); err != nil {
  348. return err
  349. }
  350. for _, fd := range fds {
  351. srcfp := filepath.Join(src, fd.Name())
  352. dstfp := filepath.Join(dst, fd.Name())
  353. if fd.IsDir() {
  354. if err = CopyDir(srcfp, dstfp); err != nil {
  355. fmt.Println(err)
  356. }
  357. } else {
  358. if err = CopyFile(srcfp, dstfp); err != nil {
  359. fmt.Println(err)
  360. }
  361. }
  362. }
  363. return nil
  364. }
  365. // CloseChrome 强行结束没有关闭的 Chrome 进程
  366. func CloseChrome(l *logrus.Logger) {
  367. cmdString := ""
  368. var command *exec.Cmd
  369. sysType := runtime.GOOS
  370. if sysType == "linux" {
  371. // LINUX系统
  372. cmdString = "pkill chrome"
  373. command = exec.Command("/bin/sh", "-c", cmdString)
  374. }
  375. if sysType == "windows" {
  376. // windows系统
  377. cmdString = "taskkill /F /im chrome.exe"
  378. command = exec.Command("cmd.exe", "/c", cmdString)
  379. }
  380. if sysType == "darwin" {
  381. // macOS
  382. // https://stackoverflow.com/questions/57079120/using-exec-command-in-golang-how-do-i-open-a-new-terminal-and-execute-a-command
  383. cmdString = `tell application "/Applications/Google Chrome.app" to quit`
  384. command = exec.Command("osascript", "-s", "h", "-e", cmdString)
  385. }
  386. if cmdString == "" || command == nil {
  387. l.Errorln("CloseChrome OS:", sysType)
  388. return
  389. }
  390. err := command.Run()
  391. if err != nil {
  392. l.Warningln("CloseChrome", err)
  393. }
  394. }
  395. // OSCheck 强制的系统支持检查
  396. func OSCheck() bool {
  397. sysType := runtime.GOOS
  398. if sysType == "linux" {
  399. return true
  400. }
  401. if sysType == "windows" {
  402. return true
  403. }
  404. if sysType == "darwin" {
  405. return true
  406. }
  407. return false
  408. }
  409. // FixWindowPathBackSlash 修复 Windows 反斜杠的梗
  410. func FixWindowPathBackSlash(path string) string {
  411. return strings.Replace(path, string(filepath.Separator), "/", -1)
  412. }
  413. func WriteStrings2File(desfilePath string, strings []string) error {
  414. dstFile, err := os.Create(desfilePath)
  415. if err != nil {
  416. return err
  417. }
  418. defer func() {
  419. _ = dstFile.Close()
  420. }()
  421. allString := ""
  422. for _, s := range strings {
  423. allString += s + "\r\n"
  424. }
  425. _, err = dstFile.WriteString(allString)
  426. if err != nil {
  427. return err
  428. }
  429. return nil
  430. }
  431. func TimeNumber2Time(inputTimeNumber float64) time.Time {
  432. newTime := time.Time{}.Add(time.Duration(inputTimeNumber * math.Pow10(9)))
  433. return newTime
  434. }
  435. func Time2SecondNumber(inTime time.Time) float64 {
  436. outSecond := 0.0
  437. outSecond += float64(inTime.Hour() * 60 * 60)
  438. outSecond += float64(inTime.Minute() * 60)
  439. outSecond += float64(inTime.Second())
  440. outSecond += float64(inTime.Nanosecond()) / 1000 / 1000 / 1000
  441. return outSecond
  442. }
  443. func Time2Duration(inTime time.Time) time.Duration {
  444. return time.Duration(Time2SecondNumber(inTime) * math.Pow10(9))
  445. }
  446. func Second2Time(sec int64) time.Time {
  447. return time.Unix(sec, 0)
  448. }
  449. // ReplaceSpecString 替换特殊的字符
  450. func ReplaceSpecString(inString string, rep string) string {
  451. return regex_things.RegMatchSpString.ReplaceAllString(inString, rep)
  452. }
  453. func Bool2Int(inBool bool) int {
  454. if inBool == true {
  455. return 1
  456. } else {
  457. return 0
  458. }
  459. }
  460. // Round 取整
  461. func Round(x float64) int64 {
  462. if x-float64(int64(x)) > 0 {
  463. return int64(x) + 1
  464. } else {
  465. return int64(x)
  466. }
  467. //return int64(math.Floor(x + 0.5))
  468. }
  469. // MakePowerOfTwo 2的整次幂数 buffer length is not a power of two
  470. func MakePowerOfTwo(x int64) int64 {
  471. power := math.Log2(float64(x))
  472. tmpRound := Round(power)
  473. return int64(math.Pow(2, float64(tmpRound)))
  474. }
  475. // MakeCeil10msMultipleFromFloat 将传入的秒,规整到 10ms 的倍数,返回依然是 秒,向上取整
  476. func MakeCeil10msMultipleFromFloat(input float64) float64 {
  477. const bb = 100
  478. // 先转到 10 ms 单位,比如传入是 1.912 - > 191.2
  479. t10ms := input * bb
  480. // 191.2 - > 192.0
  481. newT10ms := math.Ceil(t10ms)
  482. // 转换回来
  483. return newT10ms / bb
  484. }
  485. // MakeFloor10msMultipleFromFloat 将传入的秒,规整到 10ms 的倍数,返回依然是 秒,向下取整
  486. func MakeFloor10msMultipleFromFloat(input float64) float64 {
  487. const bb = 100
  488. // 先转到 10 ms 单位,比如传入是 1.912 - > 191.2
  489. t10ms := input * bb
  490. // 191.2 - > 191.0
  491. newT10ms := math.Floor(t10ms)
  492. // 转换回来
  493. return newT10ms / bb
  494. }
  495. // MakeCeil10msMultipleFromTime 向上取整,规整到 10ms 的倍数
  496. func MakeCeil10msMultipleFromTime(input time.Time) time.Time {
  497. nowTime := MakeCeil10msMultipleFromFloat(Time2SecondNumber(input))
  498. newTime := time.Time{}.Add(time.Duration(nowTime * math.Pow10(9)))
  499. return newTime
  500. }
  501. // MakeFloor10msMultipleFromTime 向下取整,规整到 10ms 的倍数
  502. func MakeFloor10msMultipleFromTime(input time.Time) time.Time {
  503. nowTime := MakeFloor10msMultipleFromFloat(Time2SecondNumber(input))
  504. newTime := time.Time{}.Add(time.Duration(nowTime * math.Pow10(9)))
  505. return newTime
  506. }
  507. // Time2SubTimeString 时间转字幕格式的时间字符串
  508. func Time2SubTimeString(inTime time.Time, timeFormat string) string {
  509. /*
  510. 这里进行时间转字符串的时候有一点比较特殊
  511. 正常来说输出的格式是类似 15:04:05.00
  512. 那么有个问题,字幕的时间格式是 0:00:12.00, 小时,是个数,除非有跨度到 20 小时的视频,不然小时就应该是个数
  513. 这就需要一个额外的函数去处理这些情况
  514. */
  515. outTimeString := inTime.Format(timeFormat)
  516. if inTime.Hour() > 9 {
  517. // 小时,两位数
  518. return outTimeString
  519. } else {
  520. // 小时,一位数
  521. items := strings.SplitN(outTimeString, ":", -1)
  522. if len(items) == 3 {
  523. outTimeString = strings.Replace(outTimeString, items[0], fmt.Sprintf("%d", inTime.Hour()), 1)
  524. return outTimeString
  525. }
  526. return outTimeString
  527. }
  528. }
  529. // IsEqual 比较 float64
  530. func IsEqual(f1, f2 float64) bool {
  531. const MIN = 0.000001
  532. if f1 > f2 {
  533. return math.Dim(f1, f2) < MIN
  534. } else {
  535. return math.Dim(f2, f1) < MIN
  536. }
  537. }
  538. // ParseTime 解析字幕时间字符串,这里可能小数点后面有 2-4 位
  539. func ParseTime(inTime string) (time.Time, error) {
  540. parseTime, err := time.Parse(common.TimeFormatPoint2, inTime)
  541. if err != nil {
  542. parseTime, err = time.Parse(common.TimeFormatPoint3, inTime)
  543. if err != nil {
  544. parseTime, err = time.Parse(common.TimeFormatPoint4, inTime)
  545. }
  546. }
  547. return parseTime, err
  548. }
  549. // GetFileSHA1 获取文件的 SHA1 值
  550. func GetFileSHA1(srcFileFPath string) (string, error) {
  551. infile, err := os.Open(srcFileFPath)
  552. if err != nil {
  553. return "", err
  554. }
  555. defer func() {
  556. _ = infile.Close()
  557. }()
  558. h := sha1.New()
  559. _, err = io.Copy(h, infile)
  560. if err != nil {
  561. return "", err
  562. }
  563. return hex.EncodeToString(h.Sum(nil)), nil
  564. }
  565. // WriteFile 写文件
  566. func WriteFile(desFileFPath string, bytes []byte) error {
  567. var err error
  568. nowDesPath := desFileFPath
  569. if filepath.IsAbs(nowDesPath) == false {
  570. nowDesPath, err = filepath.Abs(nowDesPath)
  571. if err != nil {
  572. return err
  573. }
  574. }
  575. // 创建对应的目录
  576. nowDirPath := filepath.Dir(nowDesPath)
  577. err = os.MkdirAll(nowDirPath, os.ModePerm)
  578. if err != nil {
  579. return err
  580. }
  581. file, err := os.Create(nowDesPath)
  582. if err != nil {
  583. return err
  584. }
  585. defer func() {
  586. _ = file.Close()
  587. }()
  588. _, err = file.Write(bytes)
  589. if err != nil {
  590. return err
  591. }
  592. return nil
  593. }
  594. // GetNowTimeString 获取当前的时间,没有秒
  595. func GetNowTimeString() (string, int, int, int) {
  596. nowTime := time.Now()
  597. addString := fmt.Sprintf("%d-%d-%d", nowTime.Hour(), nowTime.Minute(), nowTime.Nanosecond())
  598. return addString, nowTime.Hour(), nowTime.Minute(), nowTime.Nanosecond()
  599. }
  600. // GenerateAccessToken 生成随机的 AccessToken
  601. func GenerateAccessToken() string {
  602. u4 := uuid.New()
  603. return u4.String()
  604. }
  605. func Get2UUID() string {
  606. u4 := uuid.New()
  607. u5 := uuid.New()
  608. return u4.String() + u5.String()
  609. }
  610. func UrlJoin(hostUrl, subUrl string) (string, error) {
  611. u, err := url.Parse(hostUrl)
  612. if err != nil {
  613. return "", err
  614. }
  615. u.Path = path.Join(u.Path, subUrl)
  616. return u.String(), nil
  617. }
  618. // GetFileSHA1String 获取文件的 SHA1 字符串
  619. func GetFileSHA1String(fileFPath string) (string, error) {
  620. h := sha1.New()
  621. fp, err := os.Open(fileFPath)
  622. if err != nil {
  623. return "", err
  624. }
  625. defer func() {
  626. _ = fp.Close()
  627. }()
  628. partAll, err := io.ReadAll(fp)
  629. if err != nil {
  630. return "", err
  631. }
  632. h.Write(partAll)
  633. hashBytes := h.Sum(nil)
  634. return fmt.Sprintf("%x", md5.Sum(hashBytes)), nil
  635. }
  636. // GetFileSHA256String 获取文件的 SHA256 字符串
  637. func GetFileSHA256String(fileFPath string) (string, error) {
  638. fp, err := os.Open(fileFPath)
  639. if err != nil {
  640. return "", err
  641. }
  642. defer func() {
  643. _ = fp.Close()
  644. }()
  645. partAll, err := io.ReadAll(fp)
  646. if err != nil {
  647. return "", err
  648. }
  649. return fmt.Sprintf("%x", sha256.Sum256(partAll)), nil
  650. }
  651. func GetRestOfDaySec() time.Duration {
  652. nowTime := time.Now()
  653. todayLast := nowTime.Format("2006-01-02") + " 23:59:59"
  654. todayLastTime, _ := time.ParseInLocation("2006-01-02 15:04:05", todayLast, time.Local)
  655. // 今天剩余的时间(s)
  656. restOfDaySec := time.Duration(todayLastTime.Unix()-time.Now().Local().Unix()) * time.Second
  657. return restOfDaySec
  658. }
  659. // IntToBytes 整形转换成字节
  660. func IntToBytes(n int) ([]byte, error) {
  661. x := int32(n)
  662. bytesBuffer := bytes.NewBuffer([]byte{})
  663. err := binary.Write(bytesBuffer, binary.BigEndian, x)
  664. if err != nil {
  665. return nil, err
  666. }
  667. return bytesBuffer.Bytes(), nil
  668. }
  669. // BytesToInt 字节转换成整形
  670. func BytesToInt(b []byte) (int, error) {
  671. bytesBuffer := bytes.NewBuffer(b)
  672. var x int32
  673. err := binary.Read(bytesBuffer, binary.BigEndian, &x)
  674. if err != nil {
  675. return 0, err
  676. }
  677. return int(x), nil
  678. }
  679. var (
  680. _wantedExtMap = make(map[string]string) // 人工确认的需要监控的视频后缀名
  681. _defExtMap = make(map[string]string) // 内置支持的视频后缀名列表
  682. _customVideoExts = make([]string, 0) // 用户额外自定义的视频后缀名列表
  683. )