fns.go (3024B)
1 package utils 2 3 import ( 4 "bytes" 5 "flag" 6 "io/ioutil" 7 "log" 8 "os" 9 "os/exec" 10 "path/filepath" 11 "sort" 12 "strings" 13 14 "github.com/dhowden/tag" 15 ) 16 17 func AddCover(playlist string) { 18 filenames := [4]string{"folder.jpg", "folder.png", "cover.jpg", "cover.png"} 19 for _, filename := range filenames { 20 isCoverExists := CheckIfExists(filename) 21 if isCoverExists { 22 extIMG := "#EXTIMG: front cover\r\n" + filename + "\r\n" 23 AppendContentToFile(extIMG, playlist) 24 return 25 } 26 } 27 } 28 29 func AddToPlaylist(songs []string, cwd string) { 30 for _, song := range songs { 31 songFile, err := os.Open(song) 32 if err != nil { 33 log.Fatal(err) 34 } 35 defer songFile.Close() 36 INF := GetINFline(songFile) 37 AppendContentToFile(INF, cwd+".m3u8") 38 } 39 } 40 41 func AppendContentToFile(content string, file string) { 42 fd, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, 0644) 43 defer fd.Close() 44 if err != nil { 45 log.Fatal(err) 46 } 47 if _, err := fd.Write([]byte(content)); err != nil { 48 log.Fatal(err) 49 } 50 } 51 52 func CheckIfExists(filename string) bool { 53 _, err := os.Stat(filename) 54 return !os.IsNotExist(err) 55 } 56 57 func ErrorIfPlaylistExists(cwd string) string { 58 isOverwrite := initialiseFlags() 59 filename := cwd + ".m3u8" 60 isPlaylistExist := CheckIfExists(filename) 61 if !isPlaylistExist { 62 } else if isOverwrite { 63 os.Remove(filename) 64 } else { 65 log.Fatalf("`%s' file already exists in this directory.\n", filename) 66 } 67 return filename 68 } 69 70 func GetCWD() string { 71 pwd, err := os.Getwd() 72 if err != nil { 73 log.Fatal(err) 74 } 75 cwd := filepath.Base(pwd) 76 return cwd 77 } 78 79 func GetDuration(song *os.File) string { 80 durationCmd := exec.Command("ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", song.Name()) 81 var secs bytes.Buffer 82 durationCmd.Stdout = &secs 83 err := durationCmd.Run() 84 if err != nil { 85 log.Fatal(err) 86 } 87 rmNL := strings.Trim(secs.String(), "\n") 88 rm0 := strings.Trim(rmNL, "0") 89 rmDot := strings.Trim(rm0, ".") 90 return rmDot 91 } 92 93 func GetINFline(song *os.File) string { 94 sec := GetDuration(song) 95 artist, title := GetSongArtistTitle(song) 96 str := "#EXTINF:" + sec + ", " + artist + " - " + title + "\r\n" + song.Name() + "\r\n" 97 return str 98 } 99 100 func GetSongs() []string { 101 songs := []string{} 102 entries, err := ioutil.ReadDir(".") 103 if err != nil { 104 log.Fatal(err) 105 } 106 for _, elem := range entries { 107 name := elem.Name() 108 ext := filepath.Ext(name) 109 if ext == ".flac" || ext == ".mp3" || ext == ".wav" || ext == ".ogg" || ext == ".m4a" || ext == ".aac" { 110 songs = append(songs, name) 111 } 112 } 113 sort.Strings(songs) 114 return songs 115 } 116 117 func GetSongArtistTitle(song *os.File) (string, string) { 118 var artist string 119 var title string 120 meta, err := tag.ReadFrom(song) 121 if err != nil { 122 artist = "Unknown Artist" 123 title = "Unknown Track" 124 } else { 125 artist = meta.Artist() 126 title = meta.Title() 127 } 128 return artist, title 129 } 130 131 func initialiseFlags() bool { 132 var isOverwrite bool 133 flag.BoolVar(&isOverwrite, "overwrite", false, "Overwrite m3u8 file if it exists.") 134 flag.Parse() 135 return isOverwrite 136 }