diff --git a/backend/pkg/utils/ffmpeg.go b/backend/pkg/utils/ffmpeg.go index b96cddb..310a627 100644 --- a/backend/pkg/utils/ffmpeg.go +++ b/backend/pkg/utils/ffmpeg.go @@ -22,16 +22,16 @@ func GetMediaDuration(path string) (int64, error) { } duration := string(durationOutput) duration = strings.TrimSpace(duration) - durationInt, err := strconv.Atoi(duration) + durationFloat, err := strconv.ParseFloat(duration, 64) if err != nil { return 0, errors.Wrap(err, "duration conversion error") } - return int64(durationInt), nil + return int64(durationFloat), nil } func CutMedia(input, output string, start, end int64) error { // ffmpeg -ss 00:00:00 -i input.mp4 -to 00:01:00 -c copy output.mp4 - cmd := exec.Command("ffmpeg", "-ss", strconv.FormatInt(start, 10), "-i", input, "-t", strconv.FormatInt(end, 10), "-c", "copy", output) + cmd := exec.Command("ffmpeg", "-y", "-ss", strconv.FormatInt(start, 10), "-i", input, "-t", strconv.FormatInt(end, 10), "-c", "copy", output) stdout, err := cmd.StdoutPipe() if err != nil { @@ -81,8 +81,8 @@ func CutMedia(input, output string, start, end int64) error { // GetFrameImageFromVideo extracts target time frame from a video file and saves it as an image. func GetFrameImageFromVideo(input, output string, time int64) error { - // ffmpeg -i input.mp4 -ss 00:00:01 -vframes 1 output.jpg - cmd := exec.Command("ffmpeg", "-i", input, "-ss", strconv.FormatInt(time, 10), "-vframes", "1", output) + // ffmpeg -y -i input.mp4 -ss 00:00:01 -vframes 1 output.jpg + cmd := exec.Command("ffmpeg", "-y", "-i", input, "-ss", strconv.FormatInt(time, 10), "-vframes", "1", output) stdout, err := cmd.StdoutPipe() if err != nil { return errors.Wrap(err, "stdout pipe error") diff --git a/backend/pkg/utils/ffmpeg_test.go b/backend/pkg/utils/ffmpeg_test.go new file mode 100644 index 0000000..b99665c --- /dev/null +++ b/backend/pkg/utils/ffmpeg_test.go @@ -0,0 +1,49 @@ +package utils + +import ( + "path/filepath" + "testing" + + "github.com/rogeecn/fabfile" + . "github.com/smartystreets/goconvey/convey" +) + +func Test_GetMediaDuration(t *testing.T) { + Convey("test_get_media_duration", t, func() { + target := fabfile.MustFind("fixtures/input.mp4") + duration, err := GetMediaDuration(target) + So(err, ShouldBeNil) + t.Logf("duration is: %d", duration) + }) +} + +func Test_CutMedia(t *testing.T) { + Convey("test_cut_media", t, func() { + input := fabfile.MustFind("fixtures/input.mp4") + output := fabfile.MustFind("fixtures/output.mp4") + + t.Logf("INPUT: %s", input) + t.Logf("OUTPUT: %s", output) + + err := CutMedia(input, output, 0, 10) + So(err, ShouldBeNil) + + // check output duration + duration, err := GetMediaDuration(output) + So(err, ShouldBeNil) + t.Logf("output duration is: %d", duration) + }) +} + +func Test_GetFrameImageFromVideo(t *testing.T) { + Convey("test_get_frame_image_from_video", t, func() { + input := fabfile.MustFind("fixtures/input.mp4") + output := filepath.Join(filepath.Dir(input), "output.jpg") + + t.Logf("INPUT: %s", input) + t.Logf("OUTPUT: %s", output) + + err := GetFrameImageFromVideo(input, output, 1) + So(err, ShouldBeNil) + }) +}