util.go 23 KB

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