util.go 22 KB

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