98 lines
2.2 KiB
Go
Executable File
98 lines
2.2 KiB
Go
Executable File
package youtubedl
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
func install() error {
|
|
if _, ok := os.LookupEnv("TEST"); ok {
|
|
return nil
|
|
}
|
|
if err := installPyPip3FFMPEG(); err != nil {
|
|
return err
|
|
}
|
|
if err := installYtdl(); err != nil {
|
|
return err
|
|
}
|
|
if err := installVTT(); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func installPyPip3FFMPEG() error {
|
|
hasPy := exec.Command("python3", "--version")
|
|
hasPip := exec.Command("pip3", "--version")
|
|
hasFFMPEG := exec.Command("which", "ffmpeg")
|
|
if err := hasPy.Run(); err != nil {
|
|
} else if err := hasPip.Run(); err != nil {
|
|
} else if err := hasFFMPEG.Run(); err != nil {
|
|
} else {
|
|
return nil
|
|
}
|
|
for _, combo := range [][]string{
|
|
[]string{"sudo", "apt", "install", "python3", "pip3", "ffmpeg"},
|
|
[]string{"apt", "install", "python3", "pip3", "ffmpeg"},
|
|
[]string{"apk", "add", "python3", "py3-pip", "ffmpeg"},
|
|
[]string{"sudo", "apk", "add", "python3", "py3-pip", "ffmpeg"},
|
|
[]string{"/sbin/apk", "add", "python3", "py3-pip", "ffmpeg"},
|
|
[]string{"sudo", "/sbin/apk", "add", "python3", "py3-pip", "ffmpeg"},
|
|
} {
|
|
cmd := exec.Command(combo[0], combo[1:]...)
|
|
err := cmd.Run()
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
log.Printf("%v: %v", err, combo)
|
|
}
|
|
return errors.New("cannot get python3 and pip3 and ffmpeg")
|
|
}
|
|
|
|
func installYtdl() error {
|
|
cmd := exec.Command("/tmp/yt-dlp", "--version")
|
|
if err := cmd.Run(); err == nil {
|
|
return err
|
|
}
|
|
os.Remove("/tmp/yt-dlp")
|
|
cmd = exec.Command(
|
|
"wget",
|
|
"https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp",
|
|
"--no-check-certificate",
|
|
"-O",
|
|
"/tmp/yt-dlp",
|
|
)
|
|
if err := cmd.Run(); err != nil {
|
|
o, _ := cmd.CombinedOutput()
|
|
return fmt.Errorf("failed wget yt-dlp: %v: %s", err, o)
|
|
}
|
|
cmd = exec.Command(
|
|
"chmod",
|
|
"a+rx",
|
|
"/tmp/yt-dlp",
|
|
)
|
|
if err := cmd.Run(); err != nil {
|
|
o, _ := cmd.CombinedOutput()
|
|
return fmt.Errorf("failed chmod yt-dlp: %v: %s", err, o)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func installVTT() error {
|
|
cmd := exec.Command("python3", "-m", "vtt_to_srt")
|
|
if err := cmd.Run(); err == nil {
|
|
return err
|
|
}
|
|
cmd = exec.Command("sudo", "pip3", "install", "vtt_to_srt3")
|
|
if err := cmd.Run(); err != nil {
|
|
cmd = exec.Command("pip3", "install", "vtt_to_srt3")
|
|
if err := cmd.Run(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|