ffmpeg_helper_test.go 4.7 KB

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