-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.go
80 lines (66 loc) · 1.31 KB
/
youtube.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"clout/files"
"fmt"
"os"
"os/exec"
"time"
)
func SetupYoutubeDirectory() string {
home := files.UserHomeDir()
dir := "clout-cli-youtube"
path := home + "/" + dir
os.Mkdir(path, 0700)
return path
}
func HandleYoutube() {
id := argMap["id"]
fmt.Println(id)
action := argMap["action"]
if action == "" {
action = "download"
}
if action == "download" {
DownloadYoutube(id)
} else if action == "cut" {
CutUpFile(id)
}
}
func CutUpFile(id string) {
path := SetupYoutubeDirectory()
seconds := 60
for {
name := fmt.Sprintf("%s/%s_%05d_%05d.mp4", path, id, seconds-60, seconds)
cmd := exec.Command("ffmpeg", "-i", path+"/"+id+".mp4",
"-ss", fmt.Sprintf("%d", seconds-60),
"-t", fmt.Sprintf("%d", 60),
"-c", "copy",
name)
fmt.Println(name)
cmd.Run()
fi, _ := os.Stat(name)
size := fi.Size()
fmt.Println(size)
if size < 1000 {
break
}
seconds += 50
}
}
func DownloadYoutube(id string) {
path := SetupYoutubeDirectory()
cmd := exec.Command("youtube-dl", "--output",
path+"/%(id)s.%(ext)s",
"--recode-video", "mp4", id)
go cmd.Run()
for {
PrintDirectoryInfo(path)
time.Sleep(time.Second * 5)
}
}
func PrintDirectoryInfo(path string) {
b, _ := exec.Command("ls", "-lh", path).CombinedOutput()
s := string(b)
fmt.Println(s)
fmt.Println("")
}