feat: modify discover_medias.go and discover_medias_test.go

This commit is contained in:
Rogee
2024-12-14 23:57:34 +08:00
parent 7aebb161f6
commit 721b14372b
4 changed files with 19 additions and 37 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -1,7 +1,6 @@
package discover package discover
import ( import (
"bytes"
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"io" "io"
@@ -9,7 +8,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"backend/modules/medias" "backend/modules/medias"
@@ -73,7 +71,7 @@ func (d *DiscoverMedias) RunE(from, to string) error {
continue continue
} }
info, err := d.processVideo(video, filepath.Join(to, md5)) info, err := d.processVideo(video, to, md5)
if err != nil { if err != nil {
return errors.Wrapf(err, "process video: %s", video) return errors.Wrapf(err, "process video: %s", video)
} }
@@ -90,33 +88,37 @@ func (d *DiscoverMedias) RunE(from, to string) error {
return nil return nil
} }
func (d *DiscoverMedias) processVideo(video string, to string) (media_store.VideoInfo, error) { func (d *DiscoverMedias) processVideo(video, to, hash string) (media_store.VideoInfo, error) {
var info media_store.VideoInfo var info media_store.VideoInfo
if err := d.ensureDirectory(to); err != nil { if err := d.ensureDirectory(filepath.Join(to, hash)); err != nil {
return info, errors.Wrapf(err, "ensure directory: %s", to)
}
if err := d.ensureDirectory(filepath.Join(to, "posters")); err != nil {
return info, errors.Wrapf(err, "ensure directory: %s", to) return info, errors.Wrapf(err, "ensure directory: %s", to)
} }
// extract poster // extract poster
posterFile := filepath.Join(to, "poster.jpg") posterFile := filepath.Join(to, "posters", hash+".jpg")
if err := d.ffmpegVideoToPoster(video, posterFile); err != nil { if err := d.ffmpegVideoToPoster(video, posterFile); err != nil {
return info, errors.Wrapf(err, "ffmpeg video to poster: %s", video) return info, errors.Wrapf(err, "ffmpeg video to poster: %s", posterFile)
} }
// extract audio from video // extract audio from video
audioFile := filepath.Join(to, "audio.mp3") audioFile := filepath.Join(to, hash, "audio.mp3")
if err := d.extractAudio(video, audioFile); err != nil { if err := d.extractAudio(video, audioFile); err != nil {
return info, errors.Wrapf(err, "extract audio %s from %s", audioFile, video) return info, errors.Wrapf(err, "extract audio %s from %s", audioFile, video)
} }
defer os.Remove(audioFile) defer os.Remove(audioFile)
// ffmpeg video to m3u8 // ffmpeg video to m3u8
if err := d.ffmpegVideoToM3U8(video, to); err != nil { if err := d.ffmpegVideoToM3U8(video, to, hash); err != nil {
return info, errors.Wrapf(err, "ffmpeg video to m3u8: %s", video) return info, errors.Wrapf(err, "ffmpeg video to m3u8: %s", video)
} }
// ffmpeg audio to m3u8 // ffmpeg audio to m3u8
if err := d.ffmpegAudioToM3U8(audioFile, to); err != nil { if err := d.ffmpegAudioToM3U8(audioFile, to, hash); err != nil {
return info, errors.Wrapf(err, "ffmpeg audio to m3u8: %s", audioFile) return info, errors.Wrapf(err, "ffmpeg audio to m3u8: %s", audioFile)
} }
@@ -187,8 +189,8 @@ func (d *DiscoverMedias) ensureDirectory(dir string) error {
return nil return nil
} }
func (d *DiscoverMedias) ffmpegVideoToM3U8(input string, output string) error { func (d *DiscoverMedias) ffmpegVideoToM3U8(input, output, hash string) error {
output = filepath.Join(output, "video") output = filepath.Join(output, hash, "video")
if err := d.ensureDirectory(output); err != nil { if err := d.ensureDirectory(output); err != nil {
return errors.Wrapf(err, "ensure directory: %s", output) return errors.Wrapf(err, "ensure directory: %s", output)
} }
@@ -199,7 +201,7 @@ func (d *DiscoverMedias) ffmpegVideoToM3U8(input string, output string) error {
"-c:a", "aac", "-c:a", "aac",
"-strict", "-strict",
"-2", "-2",
"-vf", "scale=-720:576", // "-vf", "scale=-720:576",
"-f", "hls", "-f", "hls",
"-hls_time", "10", "-hls_time", "10",
"-hls_list_size", "0", "-hls_list_size", "0",
@@ -217,8 +219,8 @@ func (d *DiscoverMedias) ffmpegVideoToM3U8(input string, output string) error {
return nil return nil
} }
func (d *DiscoverMedias) ffmpegAudioToM3U8(input string, output string) error { func (d *DiscoverMedias) ffmpegAudioToM3U8(input, output, hash string) error {
output = filepath.Join(output, "audio") output = filepath.Join(output, hash, "audio")
if err := d.ensureDirectory(output); err != nil { if err := d.ensureDirectory(output); err != nil {
return errors.Wrapf(err, "ensure directory: %s", output) return errors.Wrapf(err, "ensure directory: %s", output)
} }
@@ -277,7 +279,7 @@ func (d *DiscoverMedias) ffmpegVideoToPoster(video string, output string) error
"-i", video, "-i", video,
"-ss", "00:00:01", "-ss", "00:00:01",
"-vframes", "1", "-vframes", "1",
"-vf", "scale=640:360", "-vf", "scale=1366:768 ",
output, output,
} }
@@ -291,26 +293,6 @@ func (d *DiscoverMedias) ffmpegVideoToPoster(video string, output string) error
return nil return nil
} }
func (d *DiscoverMedias) getPrice(dir string) (uint, error) {
price, err := os.ReadFile(filepath.Join(dir, "price.txt"))
if err != nil {
return 0, errors.Wrapf(err, "read price: %s", dir)
}
if len(price) == 0 {
return 0, fmt.Errorf("%s, price is empty", dir)
}
price = bytes.TrimSpace(price)
p, err := strconv.Atoi(string(price))
if err != nil {
return 0, fmt.Errorf(dir, ", price is not a number")
}
return uint(p), nil
}
// getMediaDuration get the duration of a media file // getMediaDuration get the duration of a media file
func (d *DiscoverMedias) getMediaDuration(file string) (int64, error) { func (d *DiscoverMedias) getMediaDuration(file string) (int64, error) {
// ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input_video.mp4 // ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input_video.mp4

View File

@@ -42,7 +42,7 @@ func (t *DiscoverMediasTestSuite) Test_ffmpegVideoToM3U8() {
output := "/projects/mp-qvyun/backend/fixtures/medias/abc" output := "/projects/mp-qvyun/backend/fixtures/medias/abc"
os.RemoveAll(output) os.RemoveAll(output)
err := t.Svc.ffmpegVideoToM3U8("/projects/mp-qvyun/backend/fixtures/medias/video.mp4", output) err := t.Svc.ffmpegVideoToM3U8("/projects/mp-qvyun/backend/fixtures/medias/video.mp4", output, "abc")
So(err, ShouldBeNil) So(err, ShouldBeNil)
}) })
} }