ffmpeg_helper.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. package ffmpeg_helper
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strconv"
  10. "github.com/allanpk716/ChineseSubFinder/pkg"
  11. "github.com/allanpk716/ChineseSubFinder/pkg/types/common"
  12. "github.com/allanpk716/ChineseSubFinder/pkg/language"
  13. "github.com/allanpk716/ChineseSubFinder/pkg/logic/sub_parser/ass"
  14. "github.com/allanpk716/ChineseSubFinder/pkg/logic/sub_parser/srt"
  15. "github.com/allanpk716/ChineseSubFinder/pkg/sub_parser_hub"
  16. "github.com/sirupsen/logrus"
  17. "github.com/tidwall/gjson"
  18. "strings"
  19. )
  20. type FFMPEGHelper struct {
  21. log *logrus.Logger
  22. SubParserHub *sub_parser_hub.SubParserHub // 字幕内容的解析器
  23. }
  24. func NewFFMPEGHelper(log *logrus.Logger) *FFMPEGHelper {
  25. return &FFMPEGHelper{
  26. log: log,
  27. SubParserHub: sub_parser_hub.NewSubParserHub(log, ass.NewParser(log), srt.NewParser(log)),
  28. }
  29. }
  30. // Version 获取版本信息,如果不存在 FFMPEG 和 ffprobe 则报错
  31. func (f *FFMPEGHelper) Version() (string, error) {
  32. outMsg0, err := f.getVersion("ffmpeg")
  33. if err != nil {
  34. return "", err
  35. }
  36. outMsg1, err := f.getVersion("ffprobe")
  37. if err != nil {
  38. return "", err
  39. }
  40. return outMsg0 + "\r\n" + outMsg1, nil
  41. }
  42. // GetFFMPEGInfo 获取 视频的 FFMPEG 信息,包含音频和字幕
  43. // 优先会导出 中、英、日、韩 类型的,字幕如果没有语言类型,则也导出,然后需要额外的字幕语言的判断去辅助标记(读取文件内容)
  44. // 音频只会导出一个,优先导出 中、英、日、韩 类型的
  45. func (f *FFMPEGHelper) GetFFMPEGInfo(videoFileFullPath string, exportType ExportType) (bool, *FFMPEGInfo, error) {
  46. const args = "-v error -show_format -show_streams -print_format json"
  47. cmdArgs := strings.Fields(args)
  48. cmdArgs = append(cmdArgs, videoFileFullPath)
  49. cmd := exec.Command("ffprobe", cmdArgs...)
  50. buf := bytes.NewBufferString("")
  51. //指定输出位置
  52. cmd.Stderr = buf
  53. cmd.Stdout = buf
  54. err := cmd.Start()
  55. if err != nil {
  56. return false, nil, err
  57. }
  58. err = cmd.Wait()
  59. if err != nil {
  60. return false, nil, err
  61. }
  62. // 解析得到的字符串反馈
  63. bok, ffMPEGInfo, _ := f.parseJsonString2GetFFProbeInfo(videoFileFullPath, buf.String())
  64. if bok == false {
  65. return false, nil, nil
  66. }
  67. nowCacheFolderPath, err := ffMPEGInfo.GetCacheFolderFPath()
  68. if err != nil {
  69. return false, nil, err
  70. }
  71. // 在函数调用完毕后,判断是否需要清理
  72. defer func() {
  73. if bok == false && ffMPEGInfo != nil {
  74. err := os.RemoveAll(nowCacheFolderPath)
  75. if err != nil {
  76. f.log.Errorln("GetFFMPEGInfo - RemoveAll", err.Error())
  77. return
  78. }
  79. }
  80. }()
  81. // 查找当前这个视频外置字幕列表
  82. err = ffMPEGInfo.GetExternalSubInfos(f.SubParserHub)
  83. if err != nil {
  84. return false, nil, err
  85. }
  86. ffMPEGInfo.Duration = f.getVideoDuration(videoFileFullPath)
  87. // 判断这个视频是否已经导出过内置的字幕和音频文件了
  88. if ffMPEGInfo.IsExported(exportType) == false {
  89. // 说明缓存不存在,需要导出,这里需要注意,如果导出失败了,这个文件夹要清理掉
  90. if pkg.IsDir(nowCacheFolderPath) == true {
  91. // 如果存在则,先清空一个这个文件夹
  92. err = pkg.ClearFolder(nowCacheFolderPath)
  93. if err != nil {
  94. bok = false
  95. return bok, nil, err
  96. }
  97. }
  98. // 开始导出
  99. // 构建导出的命令参数
  100. exportAudioArgs, exportSubArgs := f.getAudioAndSubExportArgs(videoFileFullPath, ffMPEGInfo)
  101. // 上面导出的信息,可能是 nil 参数,那么就直接把导出的 List 信息给置为 nil,让后续有依据可以跳出,不继续执行
  102. if exportType == Subtitle {
  103. if exportSubArgs == nil {
  104. ffMPEGInfo.SubtitleInfoList = nil
  105. return true, ffMPEGInfo, nil
  106. }
  107. } else if exportType == Audio {
  108. if exportAudioArgs == nil {
  109. ffMPEGInfo.AudioInfoList = nil
  110. return true, ffMPEGInfo, nil
  111. }
  112. } else if exportType == SubtitleAndAudio {
  113. if exportAudioArgs == nil || exportSubArgs == nil {
  114. if exportAudioArgs == nil {
  115. ffMPEGInfo.AudioInfoList = nil
  116. }
  117. if exportSubArgs == nil {
  118. ffMPEGInfo.SubtitleInfoList = nil
  119. }
  120. return true, ffMPEGInfo, nil
  121. }
  122. } else {
  123. f.log.Errorln("GetFFMPEGInfo.getAudioAndSubExportArgs Not Support ExportType")
  124. return false, nil, nil
  125. }
  126. // 上面的操作为了就是确保后续的导出不会出问题
  127. // 执行导出,音频和内置的字幕
  128. execErrorString, err := f.exportAudioAndSubtitles(exportAudioArgs, exportSubArgs, exportType)
  129. if err != nil {
  130. f.log.Errorln("exportAudioAndSubtitles", execErrorString)
  131. bok = false
  132. return bok, nil, err
  133. }
  134. // 导出后,需要把现在导出的文件的路径给复制给 ffMPEGInfo 中
  135. // 音频是否导出了
  136. ffMPEGInfo.isAudioExported(nowCacheFolderPath)
  137. // 字幕都要导出了
  138. ffMPEGInfo.isSubExported(nowCacheFolderPath)
  139. // 创建 exportedMakeFileName 这个文件
  140. // 成功,那么就需要生成这个 exportedMakeFileName 文件
  141. err = ffMPEGInfo.CreateExportedMask()
  142. if err != nil {
  143. return false, nil, err
  144. }
  145. }
  146. return bok, ffMPEGInfo, nil
  147. }
  148. // GetAudioDurationInfo 获取音频的长度信息
  149. func (f *FFMPEGHelper) GetAudioDurationInfo(audioFileFullPath string) (bool, float64, error) {
  150. const args = "-v error -show_format -show_streams -print_format json -f s16le -ac 1 -ar 16000"
  151. cmdArgs := strings.Fields(args)
  152. cmdArgs = append(cmdArgs, audioFileFullPath)
  153. cmd := exec.Command("ffprobe", cmdArgs...)
  154. buf := bytes.NewBufferString("")
  155. //指定输出位置
  156. cmd.Stderr = buf
  157. cmd.Stdout = buf
  158. err := cmd.Start()
  159. if err != nil {
  160. return false, 0, err
  161. }
  162. err = cmd.Wait()
  163. if err != nil {
  164. return false, 0, err
  165. }
  166. bok, duration := f.parseJsonString2GetAudioInfo(buf.String())
  167. if bok == false {
  168. return false, 0, errors.New("ffprobe get " + audioFileFullPath + " duration error")
  169. }
  170. return true, duration, nil
  171. }
  172. // ExportAudioAndSubArgsByTimeRange 根据输入的时间轴导出音频分段信息 "0:1:27" "28.2"
  173. func (f *FFMPEGHelper) ExportAudioAndSubArgsByTimeRange(audioFullPath, subFullPath string, startTimeString, timeLength string) (string, string, string, error) {
  174. outStartTimeString := strings.ReplaceAll(startTimeString, ":", "-")
  175. outStartTimeString = strings.ReplaceAll(outStartTimeString, ".", "#")
  176. outTimeLength := strings.ReplaceAll(timeLength, ".", "#")
  177. frontName := strings.ReplaceAll(filepath.Base(audioFullPath), filepath.Ext(audioFullPath), "")
  178. outAudioName := frontName + "_" + outStartTimeString + "_" + outTimeLength + filepath.Ext(audioFullPath)
  179. outSubName := frontName + "_" + outStartTimeString + "_" + outTimeLength + common.SubExtSRT
  180. var outAudioFullPath = filepath.Join(filepath.Dir(audioFullPath), outAudioName)
  181. var outSubFullPath = filepath.Join(filepath.Dir(audioFullPath), outSubName)
  182. // 导出音频
  183. if pkg.IsFile(outAudioFullPath) == true {
  184. err := os.Remove(outAudioFullPath)
  185. if err != nil {
  186. return "", "", "", err
  187. }
  188. }
  189. args := f.getAudioExportArgsByTimeRange(audioFullPath, startTimeString, timeLength, outAudioFullPath)
  190. execFFMPEG, err := f.execFFMPEG(args)
  191. if err != nil {
  192. return "", "", execFFMPEG, err
  193. }
  194. // 导出字幕
  195. if pkg.IsFile(outSubFullPath) == true {
  196. err := os.Remove(outSubFullPath)
  197. if err != nil {
  198. return "", "", "", err
  199. }
  200. }
  201. args = f.getSubExportArgsByTimeRange(subFullPath, startTimeString, timeLength, outSubFullPath)
  202. execFFMPEG, err = f.execFFMPEG(args)
  203. if err != nil {
  204. return "", "", execFFMPEG, err
  205. }
  206. return outAudioFullPath, outSubFullPath, "", nil
  207. }
  208. // ExportSubArgsByTimeRange 根据输入的时间轴导出字幕分段信息 "0:1:27" "28.2"
  209. func (f *FFMPEGHelper) ExportSubArgsByTimeRange(subFullPath, outName string, startTimeString, timeLength string) (string, string, error) {
  210. outStartTimeString := strings.ReplaceAll(startTimeString, ":", "-")
  211. outStartTimeString = strings.ReplaceAll(outStartTimeString, ".", "#")
  212. outTimeLength := strings.ReplaceAll(timeLength, ".", "#")
  213. frontName := strings.ReplaceAll(filepath.Base(subFullPath), filepath.Ext(subFullPath), "")
  214. outSubName := frontName + "_" + outStartTimeString + "_" + outTimeLength + "_" + outName + common.SubExtSRT
  215. var outSubFullPath = filepath.Join(filepath.Dir(subFullPath), outSubName)
  216. // 导出字幕
  217. if pkg.IsFile(outSubFullPath) == true {
  218. err := os.Remove(outSubFullPath)
  219. if err != nil {
  220. return "", "", err
  221. }
  222. }
  223. args := f.getSubExportArgsByTimeRange(subFullPath, startTimeString, timeLength, outSubFullPath)
  224. execFFMPEG, err := f.execFFMPEG(args)
  225. if err != nil {
  226. return "", execFFMPEG, err
  227. }
  228. return outSubFullPath, "", nil
  229. }
  230. // ExportVideoHLSAndSubByTimeRange 导出指定的时间轴的视频HLS和字幕,然后从 outDirPath 中获取 outputlist.m3u8 和字幕的文件
  231. func (f *FFMPEGHelper) ExportVideoHLSAndSubByTimeRange(videoFullPath, subFullPath, startTimeString, timeLength, segmentTime, outDirPath string) (string, string, error) {
  232. // 导出视频
  233. if pkg.IsFile(videoFullPath) == false {
  234. return "", "", errors.New("video file not exist, maybe is bluray file, so not support yet")
  235. }
  236. if pkg.IsFile(subFullPath) == false {
  237. return "", "", errors.New("sub file not exist")
  238. }
  239. fileName := filepath.Base(videoFullPath)
  240. frontName := strings.ReplaceAll(fileName, filepath.Ext(fileName), "")
  241. outDirSubPath := filepath.Join(outDirPath, frontName, startTimeString+"-"+timeLength)
  242. if pkg.IsDir(outDirSubPath) == true {
  243. err := os.RemoveAll(outDirSubPath)
  244. if err != nil {
  245. return "", "", err
  246. }
  247. }
  248. err := os.MkdirAll(outDirSubPath, os.ModePerm)
  249. if err != nil {
  250. return "", "", err
  251. }
  252. //// 先剪切
  253. //videoExt := filepath.Ext(fileName)
  254. //cutOffVideoFPath := filepath.Join(outDirPath, frontName+"_cut"+videoExt)
  255. //args := f.getVideoExportArgsByTimeRange(videoFullPath, startTimeString, timeLength, cutOffVideoFPath)
  256. //execFFMPEG, err := f.execFFMPEG(args)
  257. //if err != nil {
  258. // return errors.New(execFFMPEG + err.Error())
  259. //}
  260. //// 转换 HLS
  261. //args = f.getVideo2HLSArgs(cutOffVideoFPath, segmentTime, outDirPath)
  262. //execFFMPEG, err = f.execFFMPEG(args)
  263. //if err != nil {
  264. // return errors.New(execFFMPEG + err.Error())
  265. //}
  266. // 直接导出
  267. args := f.getVideoHLSExportArgsByTimeRange(videoFullPath, startTimeString, timeLength, segmentTime, outDirSubPath)
  268. execFFMPEG, err := f.execFFMPEG(args)
  269. if err != nil {
  270. return "", "", errors.New(execFFMPEG + err.Error())
  271. }
  272. // 导出字幕
  273. tmpSubFPath := subFullPath
  274. nowSubExt := filepath.Ext(tmpSubFPath)
  275. if strings.ToLower(nowSubExt) != common.SubExtSRT {
  276. // 这里需要优先判断字幕是否是 SRT,如果是 ASS 的,那么需要转换一次才行
  277. middleSubFPath := filepath.Join(outDirSubPath, frontName+"_middle"+common.SubExtSRT)
  278. args = f.getSubASS2SRTArgs(tmpSubFPath, middleSubFPath)
  279. execFFMPEG, err = f.execFFMPEG(args)
  280. if err != nil {
  281. return "", "", errors.New(execFFMPEG + err.Error())
  282. }
  283. tmpSubFPath = middleSubFPath
  284. }
  285. outSubFileFPath := filepath.Join(outDirSubPath, frontName+common.SubExtSRT)
  286. args = f.getSubExportArgsByTimeRange(tmpSubFPath, startTimeString, timeLength, outSubFileFPath)
  287. execFFMPEG, err = f.execFFMPEG(args)
  288. if err != nil {
  289. return "", "", errors.New(execFFMPEG + err.Error())
  290. }
  291. // 字幕的相对位置
  292. subRelPath, err := filepath.Rel(outDirPath, outSubFileFPath)
  293. if err != nil {
  294. return "", "", err
  295. }
  296. // outputlist.m3u8 的相对位置
  297. outputListRelPath, err := filepath.Rel(outDirPath, filepath.Join(outDirSubPath, "outputlist.m3u8"))
  298. if err != nil {
  299. return "", "", err
  300. }
  301. return outputListRelPath, subRelPath, nil
  302. }
  303. // parseJsonString2GetFFProbeInfo 使用 ffprobe 获取视频的 stream 信息,从中解析出字幕和音频的索引
  304. func (f *FFMPEGHelper) parseJsonString2GetFFProbeInfo(videoFileFullPath, inputFFProbeString string) (bool, *FFMPEGInfo, *FFMPEGInfo) {
  305. streamsValue := gjson.Get(inputFFProbeString, "streams.#")
  306. if streamsValue.Exists() == false {
  307. return false, nil, nil
  308. }
  309. ffmpegInfoFlitter := NewFFMPEGInfo(f.log, videoFileFullPath)
  310. ffmpegInfoFull := NewFFMPEGInfo(f.log, videoFileFullPath)
  311. // 进行字幕和音频的缓存,优先当然是导出 中、英、日、韩 相关的字幕和音频
  312. // 但是如果都没得这些的时候,那么也需要导出至少一个字幕或者音频,用于字幕的校正
  313. cacheAudios := make([]AudioInfo, 0)
  314. cacheSubtitleInfos := make([]SubtitleInfo, 0)
  315. for i := 0; i < int(streamsValue.Num); i++ {
  316. oneIndex := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.index", i))
  317. oneCodecName := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.codec_name", i))
  318. oneCodecType := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.codec_type", i))
  319. oneTimeBase := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.time_base", i))
  320. oneStartTime := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.start_time", i))
  321. oneLanguage := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.tags.language", i))
  322. // 任意一个字段不存在则跳过
  323. if oneIndex.Exists() == false {
  324. continue
  325. }
  326. if oneCodecName.Exists() == false {
  327. continue
  328. }
  329. if oneCodecType.Exists() == false {
  330. continue
  331. }
  332. if oneTimeBase.Exists() == false {
  333. continue
  334. }
  335. if oneStartTime.Exists() == false {
  336. continue
  337. }
  338. // 这里需要区分是字幕还是音频
  339. if oneCodecType.String() == codecTypeSub {
  340. // 字幕
  341. // 只解析 subrip 类型的,不支持 hdmv_pgs_subtitle 的字幕导出
  342. if f.isSupportSubCodecName(oneCodecName.String()) == false {
  343. continue
  344. }
  345. // 这里非必须解析到 language 字段,把所有的都导出来,然后通过额外字幕语言判断即可
  346. oneDurationTS := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.duration_ts", i))
  347. oneDuration := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.duration", i))
  348. // 必须存在的
  349. if oneDurationTS.Exists() == false {
  350. continue
  351. }
  352. if oneDuration.Exists() == false {
  353. continue
  354. }
  355. // 非必须存在的
  356. nowLanguageString := ""
  357. if oneLanguage.Exists() == true {
  358. nowLanguageString = oneLanguage.String()
  359. // 只导出 中、英、日、韩
  360. if language.IsSupportISOString(nowLanguageString) == false {
  361. subInfo := NewSubtitleInfo(int(oneIndex.Num), oneCodecName.String(), oneCodecType.String(),
  362. oneTimeBase.String(), oneStartTime.String(),
  363. int(oneDurationTS.Num), oneDuration.String(), nowLanguageString)
  364. // 不符合的也存在下来,万一,符合要求的一个都没得的时候,就需要从里面挑几个出来了
  365. cacheSubtitleInfos = append(cacheSubtitleInfos, *subInfo)
  366. continue
  367. }
  368. }
  369. subInfo := NewSubtitleInfo(int(oneIndex.Num), oneCodecName.String(), oneCodecType.String(),
  370. oneTimeBase.String(), oneStartTime.String(),
  371. int(oneDurationTS.Num), oneDuration.String(), nowLanguageString)
  372. ffmpegInfoFlitter.SubtitleInfoList = append(ffmpegInfoFlitter.SubtitleInfoList, *subInfo)
  373. } else if oneCodecType.String() == codecTypeAudio {
  374. // 音频
  375. // 这里必要要能够解析到 language 字段
  376. if oneLanguage.Exists() == false {
  377. // 不符合的也存在下来,万一,符合要求的一个都没得的时候,就需要从里面挑几个出来了
  378. audioInfo := NewAudioInfo(int(oneIndex.Num), oneCodecName.String(), oneCodecType.String(),
  379. oneTimeBase.String(), oneStartTime.String(), oneLanguage.String())
  380. cacheAudios = append(cacheAudios, *audioInfo)
  381. continue
  382. }
  383. // 只导出 中、英、日、韩
  384. if language.IsSupportISOString(oneLanguage.String()) == false {
  385. // 不符合的也存在下来,万一,符合要求的一个都没得的时候,就需要从里面挑几个出来了
  386. audioInfo := NewAudioInfo(int(oneIndex.Num), oneCodecName.String(), oneCodecType.String(),
  387. oneTimeBase.String(), oneStartTime.String(), oneLanguage.String())
  388. cacheAudios = append(cacheAudios, *audioInfo)
  389. continue
  390. }
  391. audioInfo := NewAudioInfo(int(oneIndex.Num), oneCodecName.String(), oneCodecType.String(),
  392. oneTimeBase.String(), oneStartTime.String(), oneLanguage.String())
  393. ffmpegInfoFlitter.AudioInfoList = append(ffmpegInfoFlitter.AudioInfoList, *audioInfo)
  394. } else {
  395. continue
  396. }
  397. }
  398. // 把过滤的和缓存的都拼接到一起
  399. for _, audioInfo := range ffmpegInfoFlitter.AudioInfoList {
  400. ffmpegInfoFull.AudioInfoList = append(ffmpegInfoFull.AudioInfoList, audioInfo)
  401. }
  402. for _, audioInfo := range cacheAudios {
  403. ffmpegInfoFull.AudioInfoList = append(ffmpegInfoFull.AudioInfoList, audioInfo)
  404. }
  405. for _, subInfo := range ffmpegInfoFlitter.SubtitleInfoList {
  406. ffmpegInfoFull.SubtitleInfoList = append(ffmpegInfoFull.SubtitleInfoList, subInfo)
  407. }
  408. for _, subInfo := range cacheSubtitleInfos {
  409. ffmpegInfoFull.SubtitleInfoList = append(ffmpegInfoFull.SubtitleInfoList, subInfo)
  410. }
  411. // 如何没有找到合适的字幕,那么就要把缓存的字幕选一个填充进去
  412. if len(ffmpegInfoFlitter.SubtitleInfoList) == 0 {
  413. if len(cacheSubtitleInfos) != 0 {
  414. ffmpegInfoFlitter.SubtitleInfoList = append(ffmpegInfoFlitter.SubtitleInfoList, cacheSubtitleInfos[0])
  415. }
  416. }
  417. // 如何没有找到合适的音频,那么就要把缓存的音频选一个填充进去
  418. if len(ffmpegInfoFlitter.AudioInfoList) == 0 {
  419. if len(cacheAudios) != 0 {
  420. ffmpegInfoFlitter.AudioInfoList = append(ffmpegInfoFlitter.AudioInfoList, cacheAudios[0])
  421. }
  422. } else {
  423. // 音频只需要导出一个就行了,取第一个
  424. newAudioList := make([]AudioInfo, 0)
  425. newAudioList = append(newAudioList, ffmpegInfoFlitter.AudioInfoList[0])
  426. ffmpegInfoFlitter.AudioInfoList = newAudioList
  427. }
  428. return true, ffmpegInfoFlitter, ffmpegInfoFull
  429. }
  430. // parseJsonString2GetAudioInfo 获取 pcm 音频的长度
  431. func (f *FFMPEGHelper) parseJsonString2GetAudioInfo(inputFFProbeString string) (bool, float64) {
  432. durationValue := gjson.Get(inputFFProbeString, "format.duration")
  433. if durationValue.Exists() == false {
  434. return false, 0
  435. }
  436. return true, durationValue.Float()
  437. }
  438. // exportAudioAndSubtitles 导出音频和字幕文件
  439. func (f *FFMPEGHelper) exportAudioAndSubtitles(audioArgs, subArgs []string, exportType ExportType) (string, error) {
  440. // 输入的两个数组,有可能是 nil
  441. // 这里导出依赖的是 ffmpeg 这个程序,需要的是构建导出的语句
  442. if exportType == SubtitleAndAudio {
  443. execErrorString, err := f.execFFMPEG(audioArgs)
  444. if err != nil {
  445. return execErrorString, err
  446. }
  447. execErrorString, err = f.execFFMPEG(subArgs)
  448. if err != nil {
  449. return execErrorString, err
  450. }
  451. } else if exportType == Audio {
  452. execErrorString, err := f.execFFMPEG(audioArgs)
  453. if err != nil {
  454. return execErrorString, err
  455. }
  456. } else if exportType == Subtitle {
  457. execErrorString, err := f.execFFMPEG(subArgs)
  458. if err != nil {
  459. return execErrorString, err
  460. }
  461. } else {
  462. return "", errors.New("FFMPEGHelper ExportType not support")
  463. }
  464. return "", nil
  465. }
  466. // execFFMPEG 执行 ffmpeg 命令
  467. func (f *FFMPEGHelper) execFFMPEG(cmds []string) (string, error) {
  468. if cmds == nil || len(cmds) == 0 {
  469. return "", nil
  470. }
  471. cmd := exec.Command("ffmpeg", cmds...)
  472. buf := bytes.NewBufferString("")
  473. //指定输出位置
  474. cmd.Stderr = buf
  475. cmd.Stdout = buf
  476. err := cmd.Start()
  477. if err != nil {
  478. return buf.String(), err
  479. }
  480. err = cmd.Wait()
  481. if err != nil {
  482. return buf.String(), err
  483. }
  484. return "", nil
  485. }
  486. // getAudioAndSubExportArgs 构建从原始视频导出字幕、音频的 ffmpeg 的参数 audioArgs, subArgs
  487. func (f *FFMPEGHelper) getAudioAndSubExportArgs(videoFileFullPath string, ffmpegInfo *FFMPEGInfo) ([]string, []string) {
  488. /*
  489. 导出多个字幕
  490. ffmpeg.exe -i xx.mp4 -vn -an -map 0:7 subs-7.srt -map 0:6 subs-6.srt
  491. 导出音频,从 1m 27s 开始,导出向后的 28 s,转换为 mp3 格式
  492. ffmpeg.exe -i xx.mp4 -vn -map 0:1 -ss 00:1:27 -f mp3 -t 28 audio.mp3
  493. 导出音频,转换为 mp3 格式
  494. ffmpeg.exe -i xx.mp4 -vn -map 0:1 -f mp3 audio.mp3
  495. 导出音频,转换为 16000k 16bit 单通道 采样率的 test.pcm
  496. ffmpeg.exe -i xx.mp4 -vn -map 0:1 -ss 00:1:27 -t 28 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 test.pcm
  497. 截取字幕的时间片段
  498. ffmpeg.exe -i "subs-3.srt" -ss 00:1:27 -t 28 subs-3-cut-from-org.srt
  499. */
  500. var subArgs = make([]string, 0)
  501. var audioArgs = make([]string, 0)
  502. // 基础的输入视频参数
  503. subArgs = append(subArgs, "-i")
  504. audioArgs = append(audioArgs, "-i")
  505. subArgs = append(subArgs, videoFileFullPath)
  506. audioArgs = append(audioArgs, videoFileFullPath)
  507. // 字幕导出的参数构建
  508. subArgs = append(subArgs, "-vn") // 不输出视频流
  509. subArgs = append(subArgs, "-an") // 不输出音频流
  510. nowCacheFolderPath, err := ffmpegInfo.GetCacheFolderFPath()
  511. if err != nil {
  512. f.log.Errorln("getAudioAndSubExportArgs", videoFileFullPath, err.Error())
  513. return nil, nil
  514. }
  515. if len(ffmpegInfo.SubtitleInfoList) == 0 {
  516. // 如果没有,就返回空
  517. subArgs = nil
  518. } else {
  519. for _, subtitleInfo := range ffmpegInfo.SubtitleInfoList {
  520. f.addSubMapArg(&subArgs, subtitleInfo.Index,
  521. filepath.Join(nowCacheFolderPath, subtitleInfo.GetName()+common.SubExtSRT))
  522. f.addSubMapArg(&subArgs, subtitleInfo.Index,
  523. filepath.Join(nowCacheFolderPath, subtitleInfo.GetName()+common.SubExtASS))
  524. }
  525. }
  526. // 音频导出的参数构建
  527. audioArgs = append(audioArgs, "-vn")
  528. if len(ffmpegInfo.AudioInfoList) == 0 {
  529. // 如果没有,就返回空
  530. audioArgs = nil
  531. } else {
  532. for _, audioInfo := range ffmpegInfo.AudioInfoList {
  533. f.addAudioMapArg(&audioArgs, audioInfo.Index,
  534. filepath.Join(nowCacheFolderPath, audioInfo.GetName()+extPCM))
  535. }
  536. }
  537. return audioArgs, subArgs
  538. }
  539. // getVideoExportArgsByTimeRange 导出某个时间范围内的视频 startTimeString 00:1:27 timeLeng 向后多少秒
  540. func (f *FFMPEGHelper) getVideoExportArgsByTimeRange(videoFullPath string, startTimeString, timeLeng, outVideiFullPath string) []string {
  541. /*
  542. 这个是用 to 到那个时间点
  543. ffmpeg.exe -i '.\Chainsaw Man - S01E02 - ARRIVAL IN TOKYO HDTV-1080p.mp4' -ss 00:00:00 -to 00:05:00 -c:v copy -c:a copy wawa.mp4
  544. 这个是向后多少秒
  545. ffmpeg.exe -i '.\Chainsaw Man - S01E02 - ARRIVAL IN TOKYO HDTV-1080p.mp4' -ss 00:00:00 -t 300 -c:v copy -c:a copy wawa.mp4
  546. */
  547. videoArgs := make([]string, 0)
  548. videoArgs = append(videoArgs, "-ss")
  549. videoArgs = append(videoArgs, startTimeString)
  550. videoArgs = append(videoArgs, "-t")
  551. videoArgs = append(videoArgs, timeLeng)
  552. // 解决开头黑屏问题
  553. videoArgs = append(videoArgs, "-accurate_seek")
  554. videoArgs = append(videoArgs, "-i")
  555. videoArgs = append(videoArgs, videoFullPath)
  556. videoArgs = append(videoArgs, "-c:v")
  557. videoArgs = append(videoArgs, "copy")
  558. videoArgs = append(videoArgs, "-c:a")
  559. videoArgs = append(videoArgs, "copy")
  560. videoArgs = append(videoArgs, outVideiFullPath)
  561. return videoArgs
  562. }
  563. // getVideoHLSExportArgsByTimeRange 导出某个时间范围内的视频的 HLS 信息 startTimeString 00:1:27 timeLeng 向后多少秒
  564. func (f *FFMPEGHelper) getVideoHLSExportArgsByTimeRange(videoFullPath string, startTimeString, timeLeng, sgmentTime, outVideiDirPath string) []string {
  565. /*
  566. ffmpeg.exe -i '111.mp4' -ss 00:00:00 -to 00:05:00 -c:v copy -c:a copy -f segment -segment_time 10 -segment_list outputlist.m3u8 -segment_format mpegts output%03d.ts
  567. */
  568. videoArgs := make([]string, 0)
  569. videoArgs = append(videoArgs, "-ss")
  570. videoArgs = append(videoArgs, startTimeString)
  571. videoArgs = append(videoArgs, "-t")
  572. videoArgs = append(videoArgs, timeLeng)
  573. // 解决开头黑屏问题
  574. videoArgs = append(videoArgs, "-accurate_seek")
  575. videoArgs = append(videoArgs, "-i")
  576. videoArgs = append(videoArgs, videoFullPath)
  577. videoArgs = append(videoArgs, "-force_key_frames")
  578. videoArgs = append(videoArgs, "\"expr:gte(t,n_forced*"+sgmentTime+")\"")
  579. videoArgs = append(videoArgs, "-c:v")
  580. videoArgs = append(videoArgs, "copy")
  581. videoArgs = append(videoArgs, "-c:a")
  582. videoArgs = append(videoArgs, "copy")
  583. videoArgs = append(videoArgs, "-f")
  584. videoArgs = append(videoArgs, "segment")
  585. videoArgs = append(videoArgs, "-segment_time")
  586. videoArgs = append(videoArgs, sgmentTime)
  587. videoArgs = append(videoArgs, "-segment_list")
  588. videoArgs = append(videoArgs, filepath.Join(outVideiDirPath, "outputlist.m3u8"))
  589. videoArgs = append(videoArgs, "-segment_format")
  590. videoArgs = append(videoArgs, "mpegts")
  591. videoArgs = append(videoArgs, filepath.Join(outVideiDirPath, "output%03d.ts"))
  592. return videoArgs
  593. }
  594. func (f *FFMPEGHelper) getVideo2HLSArgs(videoFullPath, segmentTime, outVideoDirPath string) []string {
  595. /*
  596. ffmpeg.exe -i '111.mp4' -c copy -map 0 -f segment -segment_list playlist.m3u8 -segment_time 10 -segment_format mpegts output%03d.ts
  597. */
  598. videoArgs := make([]string, 0)
  599. videoArgs = append(videoArgs, "-i")
  600. videoArgs = append(videoArgs, videoFullPath)
  601. videoArgs = append(videoArgs, "-force_key_frames")
  602. videoArgs = append(videoArgs, "\"expr:gte(t,n_forced*"+segmentTime+")\"")
  603. videoArgs = append(videoArgs, "-c:v")
  604. videoArgs = append(videoArgs, "copy")
  605. videoArgs = append(videoArgs, "-c:a")
  606. videoArgs = append(videoArgs, "copy")
  607. videoArgs = append(videoArgs, "-f")
  608. videoArgs = append(videoArgs, "segment")
  609. videoArgs = append(videoArgs, "-segment_list")
  610. videoArgs = append(videoArgs, filepath.Join(outVideoDirPath, "playlist.m3u8"))
  611. videoArgs = append(videoArgs, "-segment_time")
  612. videoArgs = append(videoArgs, segmentTime)
  613. videoArgs = append(videoArgs, "-segment_format")
  614. videoArgs = append(videoArgs, "mpegts")
  615. videoArgs = append(videoArgs, filepath.Join(outVideoDirPath, "output%03d.ts"))
  616. return videoArgs
  617. }
  618. // getAudioAndSubExportArgsByTimeRange 导出某个时间范围内的音频和字幕文件文件 startTimeString 00:1:27 timeLeng 向后多少秒
  619. func (f *FFMPEGHelper) getAudioExportArgsByTimeRange(audioFullPath string, startTimeString, timeLeng, outAudioFullPath string) []string {
  620. /*
  621. ffmpeg.exe -ar 16000 -ac 1 -f s16le -i aa.pcm -ss 00:1:27 -t 28 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 bb.pcm
  622. ffmpeg.exe -i aa.srt -ss 00:1:27 -t 28 bb.srt
  623. */
  624. var audioArgs = make([]string, 0)
  625. // 指定读取的音频文件编码格式
  626. audioArgs = append(audioArgs, "-ar")
  627. audioArgs = append(audioArgs, "16000")
  628. audioArgs = append(audioArgs, "-ac")
  629. audioArgs = append(audioArgs, "1")
  630. audioArgs = append(audioArgs, "-f")
  631. audioArgs = append(audioArgs, "s16le")
  632. audioArgs = append(audioArgs, "-i")
  633. audioArgs = append(audioArgs, audioFullPath)
  634. audioArgs = append(audioArgs, "-ss")
  635. audioArgs = append(audioArgs, startTimeString)
  636. audioArgs = append(audioArgs, "-t")
  637. audioArgs = append(audioArgs, timeLeng)
  638. // 指定导出的音频文件编码格式
  639. audioArgs = append(audioArgs, "-acodec")
  640. audioArgs = append(audioArgs, "pcm_s16le")
  641. audioArgs = append(audioArgs, "-f")
  642. audioArgs = append(audioArgs, "s16le")
  643. audioArgs = append(audioArgs, "-ac")
  644. audioArgs = append(audioArgs, "1")
  645. audioArgs = append(audioArgs, "-ar")
  646. audioArgs = append(audioArgs, "16000")
  647. audioArgs = append(audioArgs, outAudioFullPath)
  648. return audioArgs
  649. }
  650. func (f *FFMPEGHelper) getSubExportArgsByTimeRange(subFullPath string, startTimeString, timeLength, outSubFullPath string) []string {
  651. /*
  652. ffmpeg.exe -i aa.srt -ss 00:1:27 -t 28 bb.srt
  653. */
  654. var subArgs = make([]string, 0)
  655. subArgs = append(subArgs, "-i")
  656. subArgs = append(subArgs, subFullPath)
  657. subArgs = append(subArgs, "-ss")
  658. subArgs = append(subArgs, startTimeString)
  659. subArgs = append(subArgs, "-t")
  660. subArgs = append(subArgs, timeLength)
  661. subArgs = append(subArgs, outSubFullPath)
  662. return subArgs
  663. }
  664. // getSubASS2SRTArgs 从 ASS 字幕 转到 SRT 字幕
  665. func (f *FFMPEGHelper) getSubASS2SRTArgs(subFullPath, outSubFullPath string) []string {
  666. var subArgs = make([]string, 0)
  667. // 指定读取的音频文件编码格式
  668. subArgs = append(subArgs, "-i")
  669. subArgs = append(subArgs, subFullPath)
  670. subArgs = append(subArgs, outSubFullPath)
  671. return subArgs
  672. }
  673. // addSubMapArg 构建字幕的导出参数
  674. func (f *FFMPEGHelper) addSubMapArg(subArgs *[]string, index int, subSaveFullPath string) {
  675. *subArgs = append(*subArgs, "-map")
  676. *subArgs = append(*subArgs, fmt.Sprintf("0:%d", index))
  677. *subArgs = append(*subArgs, subSaveFullPath)
  678. }
  679. // addAudioMapArg 构建音频的导出参数
  680. func (f *FFMPEGHelper) addAudioMapArg(subArgs *[]string, index int, audioSaveFullPath string) {
  681. // -acodec pcm_s16le -f s16le -ac 1 -ar 16000
  682. *subArgs = append(*subArgs, "-map")
  683. *subArgs = append(*subArgs, fmt.Sprintf("0:%d", index))
  684. *subArgs = append(*subArgs, "-acodec")
  685. *subArgs = append(*subArgs, "pcm_s16le")
  686. *subArgs = append(*subArgs, "-f")
  687. *subArgs = append(*subArgs, "s16le")
  688. *subArgs = append(*subArgs, "-ac")
  689. *subArgs = append(*subArgs, "1")
  690. *subArgs = append(*subArgs, "-ar")
  691. *subArgs = append(*subArgs, "16000")
  692. *subArgs = append(*subArgs, audioSaveFullPath)
  693. }
  694. func (f *FFMPEGHelper) getVersion(exeName string) (string, error) {
  695. const args = "-version"
  696. cmdArgs := strings.Fields(args)
  697. cmd := exec.Command(exeName, cmdArgs...)
  698. buf := bytes.NewBufferString("")
  699. //指定输出位置
  700. cmd.Stderr = buf
  701. cmd.Stdout = buf
  702. err := cmd.Start()
  703. if err != nil {
  704. return "", err
  705. }
  706. err = cmd.Wait()
  707. if err != nil {
  708. return "", err
  709. }
  710. return buf.String(), nil
  711. }
  712. // isSupportSubCodecName 是否是 FFMPEG 支持的 CodecName
  713. func (f *FFMPEGHelper) isSupportSubCodecName(name string) bool {
  714. switch name {
  715. case Subtitle_StreamCodec_subrip,
  716. Subtitle_StreamCodec_ass,
  717. Subtitle_StreamCodec_ssa,
  718. Subtitle_StreamCodec_srt:
  719. return true
  720. default:
  721. return false
  722. }
  723. }
  724. func (f *FFMPEGHelper) getVideoDuration(videoFileFullPath string) float64 {
  725. const args = "-v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -i"
  726. cmdArgs := strings.Fields(args)
  727. cmdArgs = append(cmdArgs, videoFileFullPath)
  728. cmd := exec.Command("ffprobe", cmdArgs...)
  729. buf := bytes.NewBufferString("")
  730. //指定输出位置
  731. cmd.Stderr = buf
  732. cmd.Stdout = buf
  733. err := cmd.Start()
  734. if err != nil {
  735. return 0
  736. }
  737. err = cmd.Wait()
  738. if err != nil {
  739. return 0
  740. }
  741. // 字符串转 float64
  742. durationStr := strings.TrimSpace(buf.String())
  743. duration, err := strconv.ParseFloat(durationStr, 32)
  744. if err != nil {
  745. return 0
  746. }
  747. return duration
  748. }
  749. const (
  750. codecTypeSub = "subtitle"
  751. codecTypeAudio = "audio"
  752. extMP3 = ".mp3"
  753. extPCM = ".pcm"
  754. )
  755. type ExportType int
  756. const (
  757. Subtitle ExportType = iota // 导出字幕
  758. Audio // 导出音频
  759. SubtitleAndAudio // 导出字幕和音频
  760. )
  761. /*
  762. FFMPEG 支持的字幕 Codec Name:
  763. ..S... arib_caption ARIB STD-B24 caption
  764. DES... ass ASS (Advanced SSA) subtitle (decoders: ssa ass ) (encoders: ssa ass )
  765. DES... dvb_subtitle DVB subtitles (decoders: dvbsub ) (encoders: dvbsub )
  766. ..S... dvb_teletext DVB teletext
  767. DES... dvd_subtitle DVD subtitles (decoders: dvdsub ) (encoders: dvdsub )
  768. D.S... eia_608 EIA-608 closed captions (decoders: cc_dec )
  769. D.S... hdmv_pgs_subtitle HDMV Presentation Graphic Stream subtitles (decoders: pgssub )
  770. ..S... hdmv_text_subtitle HDMV Text subtitle
  771. D.S... jacosub JACOsub subtitle
  772. D.S... microdvd MicroDVD subtitle
  773. DES... mov_text MOV text
  774. D.S... mpl2 MPL2 subtitle
  775. D.S... pjs PJS (Phoenix Japanimation Society) subtitle
  776. D.S... realtext RealText subtitle
  777. D.S... sami SAMI subtitle
  778. ..S... srt SubRip subtitle with embedded timing
  779. ..S... ssa SSA (SubStation Alpha) subtitle
  780. D.S... stl Spruce subtitle format
  781. DES... subrip SubRip subtitle (decoders: srt subrip ) (encoders: srt subrip )
  782. D.S... subviewer SubViewer subtitle
  783. D.S... subviewer1 SubViewer v1 subtitle
  784. DES... text raw UTF-8 text
  785. ..S... ttml Timed Text Markup Language
  786. D.S... vplayer VPlayer subtitle
  787. DES... webvtt WebVTT subtitle
  788. DES... xsub XSUB
  789. */
  790. const (
  791. Subtitle_StreamCodec_subrip = "subrip"
  792. Subtitle_StreamCodec_ass = "ass"
  793. Subtitle_StreamCodec_ssa = "ssa"
  794. Subtitle_StreamCodec_srt = "srt"
  795. )