-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (84 loc) · 2.02 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"bazil.org/fuse"
"bazil.org/fuse/fs"
_ "bazil.org/fuse/fs/fstestutil"
"github.com/rafalb8/ytfs-go/client"
"github.com/rafalb8/ytfs-go/filesystem"
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s MOUNTPOINT PLAYLIST_URL\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
flag.StringVar(&client.SelectedAudioFormat, "a", "aac", "Set audio format (aac, opus, wav, mp3)")
flag.Usage = usage
flag.Parse()
defFormat, exists := client.AudioFormatMap[client.SelectedAudioFormat]
if flag.NArg() != 2 || !exists {
usage()
os.Exit(2)
}
mountpoint := flag.Arg(0)
playlistURL := flag.Arg(1)
// Get Playlist videos
playlist, err := client.GetPlaylist(playlistURL)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
bytesPerSec := 16000
if client.SelectedAudioFormat == "wav" {
bytesPerSec = 176400
}
files := map[string]*filesystem.File{}
dirEntries := []fuse.Dirent{}
fmt.Println("Videos found:")
for i, entry := range playlist.Videos {
title := strings.ReplaceAll(entry.Title, "/", "|")
// Create dir entry
dirEntry := fuse.Dirent{
Name: title + "." + defFormat.Extension,
Inode: uint64(i + 2),
Type: fuse.DT_Block,
}
dirEntries = append(dirEntries, dirEntry)
// Create file entry
files[dirEntry.Name] = &filesystem.File{
Title: title,
Inode: dirEntry.Inode,
PlaylistEntry: entry,
Size: uint64(int(entry.Duration.Seconds()+1) * bytesPerSec),
}
fmt.Printf("%d. %s\n", i+1, title)
}
fs := &filesystem.FS{
Files: files,
DIREntries: dirEntries,
}
run(mountpoint, fs)
}
func run(mountpoint string, filesys *filesystem.FS) {
c, err := fuse.Mount(
mountpoint,
fuse.FSName("youtubefs"),
fuse.Subtype("ytfs-go"),
)
if err != nil {
log.Fatal(err)
}
defer c.Close()
fmt.Println("\nStarting filesystem")
err = fs.Serve(c, filesys)
if err != nil {
log.Fatal(err)
}
fmt.Println("Stopping filesystem")
// fuse.Unmount(mountpoint)
}