136 lines
4.2 KiB
Go
136 lines
4.2 KiB
Go
|
package mods
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/url"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"cdmnky.io/net/patreon-ui/local.pkg/src/config"
|
||
|
vimeo "cdmnky.io/net/vimeo/pkg.local/vimeo/src"
|
||
|
"go.cdmnky.io/v2/str"
|
||
|
"go.cdmnky.io/v2/utils"
|
||
|
)
|
||
|
|
||
|
func ProcVimeo(fn func(string), config *config.Config, playlistURL string, outfile string, maxSize int64) {
|
||
|
|
||
|
sleepTimeout := 5
|
||
|
|
||
|
videofile := fmt.Sprintf("/tmp/%s.mp4", str.Random(12))
|
||
|
audiofile := fmt.Sprintf("/tmp/%s.mp3", str.Random(12))
|
||
|
tmpfile := fmt.Sprintf("/tmp/%s.mp4", str.Random(12))
|
||
|
encfile := fmt.Sprintf("/tmp/%s.mp4", str.Random(12))
|
||
|
idxStart := "1"
|
||
|
|
||
|
now := time.Now()
|
||
|
|
||
|
fmt.Printf("Work Video File............. %s\n", videofile)
|
||
|
fmt.Printf("Work Audio File............. %s\n", audiofile)
|
||
|
fmt.Printf("Work Temp File.............. %s\n", tmpfile)
|
||
|
fmt.Printf("Work Encoded File........... %s\n", encfile)
|
||
|
fmt.Printf("Start Index................. %s\n", idxStart)
|
||
|
fmt.Printf("Outfile..................... %s\n", outfile)
|
||
|
|
||
|
data, err := Dload(playlistURL)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
objVimeo := vimeo.Vimeo{}
|
||
|
_, err = objVimeo.Unmarshal(data)
|
||
|
if err != nil {
|
||
|
fn(Format("Error: %v", err))
|
||
|
time.Sleep(time.Duration(sleepTimeout) * time.Second)
|
||
|
}
|
||
|
|
||
|
playlistURL = playlistURL[:strings.Index(playlistURL, "?")]
|
||
|
baseURL, _ := url.JoinPath(playlistURL, "../", objVimeo.BaseURL)
|
||
|
_ = baseURL
|
||
|
|
||
|
success := 0
|
||
|
|
||
|
idxs, err := strconv.Atoi(idxStart)
|
||
|
if err != nil {
|
||
|
fn(Format("Error: %v", err))
|
||
|
time.Sleep(time.Duration(sleepTimeout) * time.Second)
|
||
|
}
|
||
|
fmt.Println("Fetching video data")
|
||
|
time.Sleep(3 * time.Second)
|
||
|
for idx := idxs; idx < len(objVimeo.Video); idx++ {
|
||
|
URL, _ := url.JoinPath(baseURL, objVimeo.Video[idx].BaseURL, objVimeo.Video[idx].IndexSegment)
|
||
|
fmt.Printf("Trying [%d/%d] %s...\n", idx, len(objVimeo.Video), objVimeo.Video[idx].ID)
|
||
|
fn(Format("Fetching video data: Trying [%d/%d] %s...", idx, len(objVimeo.Video), objVimeo.Video[idx].ID))
|
||
|
if err := ProcessVideo(URL, videofile); err != nil {
|
||
|
fn(Format("Error: %v", err))
|
||
|
time.Sleep(time.Duration(sleepTimeout) * time.Second)
|
||
|
} else {
|
||
|
success++
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
GetFileSize("Video file", videofile)
|
||
|
|
||
|
fmt.Println("Fetching audio data")
|
||
|
time.Sleep(3 * time.Second)
|
||
|
for idx := 1; idx < len(objVimeo.Audio); idx++ {
|
||
|
URL, _ := url.JoinPath(baseURL, objVimeo.Audio[idx].BaseURL, objVimeo.Audio[idx].IndexSegment)
|
||
|
fmt.Printf("Trying [%d/%d] %s...\n", idx+1, len(objVimeo.Audio), objVimeo.Audio[idx].ID)
|
||
|
fn(Format("Fetching audio data: Trying [%d/%d] %s...", idx+1, len(objVimeo.Audio), objVimeo.Audio[idx].ID))
|
||
|
if err := ProcessAudio(URL, audiofile); err != nil {
|
||
|
fn(Format("Error: %v", err))
|
||
|
time.Sleep(time.Duration(sleepTimeout) * time.Second)
|
||
|
} else {
|
||
|
success++
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
GetFileSize("Audio file", audiofile)
|
||
|
|
||
|
if success != 2 {
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
fmt.Printf("Merging audio/video data into temp file '%s'...\n", tmpfile)
|
||
|
fn(Format("Merging audio/video data into temp file '%s'...", tmpfile))
|
||
|
if err = Merge(config.FFMpegBin, videofile, audiofile, tmpfile); err != nil {
|
||
|
fn(Format("Error: %v", err))
|
||
|
time.Sleep(time.Duration(sleepTimeout) * time.Second)
|
||
|
}
|
||
|
|
||
|
fileSize := GetFileSize("Temp file", tmpfile)
|
||
|
if maxSize > 0 && fileSize > int64(maxSize) {
|
||
|
if err = Encode(config.FFMpegBin, []string{tmpfile}); err != nil {
|
||
|
fn(Format("Error: %v", err))
|
||
|
time.Sleep(time.Duration(sleepTimeout) * time.Second)
|
||
|
}
|
||
|
} else {
|
||
|
fmt.Println("File size does not exceed max file size; continuing...")
|
||
|
}
|
||
|
|
||
|
fmt.Printf("Renaming '%s' to '%s'...\n", tmpfile, outfile)
|
||
|
os.Rename(tmpfile, outfile)
|
||
|
GetFileSize("Output file", outfile)
|
||
|
|
||
|
//if override || x.Prompt("Cleanup work files (y/n)? ") == "y" {
|
||
|
fmt.Printf("Removing '%s'...\n", videofile)
|
||
|
if err = os.Remove(videofile); err != nil {
|
||
|
fn(Format("Error: %v", err))
|
||
|
time.Sleep(time.Duration(sleepTimeout) * time.Second)
|
||
|
}
|
||
|
fmt.Printf("Removing '%s'...\n", audiofile)
|
||
|
if err = os.Remove(audiofile); err != nil {
|
||
|
fn(Format("Error: %v", err))
|
||
|
time.Sleep(time.Duration(sleepTimeout) * time.Second)
|
||
|
}
|
||
|
//}
|
||
|
|
||
|
elapsedTime := utils.ElapsedTime(now)
|
||
|
fmt.Printf("Elapsed time: %s\n", elapsedTime)
|
||
|
|
||
|
fmt.Printf("Download complete. Filename: '%s', Elapsed time: %s", outfile, elapsedTime)
|
||
|
fn(Format("Complete. Elapsed time: %s\n", elapsedTime))
|
||
|
}
|