ffmpeg_helper_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package ffmpeg_helper
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "time"
  7. "github.com/allanpk716/ChineseSubFinder/pkg"
  8. "github.com/allanpk716/ChineseSubFinder/pkg/log_helper"
  9. "github.com/allanpk716/ChineseSubFinder/pkg/unit_test_helper"
  10. )
  11. func TestGetFFMPEGInfo(t *testing.T) {
  12. // use small video sample form google
  13. // TODO: make a video with ffmpeg on each test
  14. // https://gist.github.com/SeunghoonBaek/f35e0fd3db80bf55c2707cae5d0f7184
  15. // http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4
  16. videoFile := unit_test_helper.GetTestDataResourceRootPath([]string{"ffmpeg", "org"}, 4, false)
  17. videoFile = filepath.Join(videoFile, "sampleVideo.mp4")
  18. f := NewFFMPEGHelper(log_helper.GetLogger4Tester())
  19. bok, ffmpegInfo, err := f.ExportFFMPEGInfo(videoFile, Audio)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if bok == false {
  24. t.Fatal("ExportFFMPEGInfo = false")
  25. }
  26. subArgs, audioArgs := f.getAudioAndSubExportArgs(videoFile, ffmpegInfo)
  27. t.Logf("\n\nsubArgs: %d audioArgs: %d\n", len(subArgs), len(audioArgs))
  28. }
  29. func readString(filePath string) string {
  30. bytes, err := os.ReadFile(filePath)
  31. if err != nil {
  32. return ""
  33. }
  34. return string(bytes)
  35. }
  36. func TestParseJsonString2GetFFMPEGInfo(t *testing.T) {
  37. testDataPath := unit_test_helper.GetTestDataResourceRootPath([]string{"ffmpeg", "org"}, 4, false)
  38. type args struct {
  39. videoFileFullPath string
  40. input string
  41. }
  42. tests := []struct {
  43. name string
  44. args args
  45. want bool
  46. subsFilter int
  47. audiosFilter int
  48. subsFull int
  49. audiosFull int
  50. }{
  51. {name: "R&M S05E10", args: args{videoFileFullPath: "123", input: readString(filepath.Join(testDataPath, "R&M S05E10-video_stream.json"))},
  52. want: true, subsFilter: 1, audiosFilter: 1, subsFull: 1, audiosFull: 1},
  53. {name: "千与千寻", args: args{videoFileFullPath: "123", input: readString(filepath.Join(testDataPath, "千与千寻-video_stream.json"))},
  54. want: true, subsFilter: 2, audiosFilter: 1, subsFull: 2, audiosFull: 3},
  55. }
  56. f := NewFFMPEGHelper(log_helper.GetLogger4Tester())
  57. for _, tt := range tests {
  58. t.Run(tt.name, func(t *testing.T) {
  59. got, got1, got2 := f.parseJsonString2GetFFProbeInfo(tt.args.videoFileFullPath, tt.args.input)
  60. if got != tt.want {
  61. t.Errorf("parseJsonString2GetFFProbeInfo() got = %v, want %v", got, tt.want)
  62. }
  63. if len(got1.AudioInfoList) != tt.audiosFilter || len(got1.SubtitleInfoList) != tt.subsFilter {
  64. t.Errorf("\n\n%s Num. Audio: %d (%d) Num. Subtitles: %d (%d)", tt.name, len(got1.AudioInfoList), tt.audiosFilter, len(got1.SubtitleInfoList), tt.subsFilter)
  65. t.Fatal("parseJsonString2GetFFProbeInfo result List < 1")
  66. }
  67. if len(got2.AudioInfoList) != tt.audiosFull || len(got2.SubtitleInfoList) != tt.subsFull {
  68. t.Errorf("\n\n%s Num. Audio: %d (%d) Num. Subtitles: %d (%d)", tt.name, len(got2.AudioInfoList), tt.audiosFull, len(got2.SubtitleInfoList), tt.subsFull)
  69. t.Fatal("parseJsonString2GetFFProbeInfo result List < 1")
  70. }
  71. })
  72. }
  73. }
  74. func TestExportAudioArgsByTimeRange(t *testing.T) {
  75. // https://www.lynxstudio.com/downloads/e44/sample-wav-file-zip-encoded-44-1khz-pcm-24-stereo/
  76. // TODO: make a sample audio file with ffmpeg
  77. // TODO: remove generated audio files
  78. testDataPath := unit_test_helper.GetTestDataResourceRootPath([]string{"ffmpeg"}, 4, true)
  79. audioFullPath := filepath.Join(testDataPath, "sampleAudio.wav")
  80. subFullPath := filepath.Join(testDataPath, "sampleSrt.srt")
  81. startTimeString := "0:0:27"
  82. timeLeng := "28.2"
  83. f := NewFFMPEGHelper(log_helper.GetLogger4Tester())
  84. _, _, timeRange, err := f.ExportAudioAndSubArgsByTimeRange(audioFullPath, subFullPath, startTimeString, timeLeng)
  85. if err != nil {
  86. t.Logf("\n\nTime Range: %s", timeRange)
  87. t.Fatal(err)
  88. }
  89. }
  90. func TestGetAudioInfo(t *testing.T) {
  91. testDataPath := unit_test_helper.GetTestDataResourceRootPath([]string{"ffmpeg", "org"}, 4, false)
  92. audioFullPath := filepath.Join(testDataPath, "sampleAudio.wav")
  93. f := NewFFMPEGHelper(log_helper.GetLogger4Tester())
  94. bok, duration, err := f.ExportAudioDurationInfo(audioFullPath)
  95. if err != nil || bok == false {
  96. t.Fatal(err)
  97. }
  98. t.Logf("\n\nAudio Duration: %f\n", duration)
  99. }
  100. func TestVersion(t *testing.T) {
  101. f := FFMPEGHelper{}
  102. _, err := f.Version()
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. t.Logf("\n\nGet ffmpeg/ffprobe version\n")
  107. }
  108. func TestExportVideoHLSAndSubByTimeRange(t *testing.T) {
  109. outDirPath := "C:\\Tmp\\media\\test\\hls"
  110. videoFPath := "C:\\Tmp\\media\\test\\Chainsaw Man - S01E02 - ARRIVAL IN TOKYO HDTV-1080p.mp4"
  111. subFPaths := []string{
  112. "C:\\Tmp\\media\\test\\Chainsaw Man - S01E02 - ARRIVAL IN TOKYO HDTV-1080p.chinese(简,csf).default.srt",
  113. "C:\\Tmp\\media\\test\\Chainsaw Man - S01E01 - DOG & CHAINSAW WEBRip-1080p.chinese(简,csf).default.srt",
  114. }
  115. f := NewFFMPEGHelper(log_helper.GetLogger4Tester())
  116. println("Start:", time.Now().Format("2006-01-02 15:04:05"))
  117. m3u8, subs, err := f.ExportVideoHLSAndSubByTimeRange(videoFPath, subFPaths, "10", "10", "5.000", outDirPath)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. println("Start:", time.Now().Format("2006-01-02 15:04:05"))
  122. if pkg.IsFile(filepath.Join(outDirPath, m3u8)) == false {
  123. t.Fatal("m3u8 file not found")
  124. }
  125. for _, path := range subs {
  126. if pkg.IsFile(filepath.Join(outDirPath, path)) == false {
  127. t.Fatal("sub file not found")
  128. }
  129. }
  130. }