ffmpeg_helper_test.go 4.0 KB

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