From 42cd0865a57aa5840ddbed6ec58d708c9dfdbd0f Mon Sep 17 00:00:00 2001
From: Khairul Hidayat <me@khairul.my.id>
Date: Sat, 12 Oct 2024 09:14:36 +0000
Subject: [PATCH] fix: fix error video format not available

---
 .air.toml     |  2 +-
 lib/tasks.go  |  5 +++++
 lib/yt2mp3.go | 12 +++++++-----
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/.air.toml b/.air.toml
index 58fff2a..cc52c5f 100644
--- a/.air.toml
+++ b/.air.toml
@@ -7,7 +7,7 @@ tmp_dir = "tmp"
   bin = "./tmp/main"
   cmd = "go build -o ./tmp/main ."
   delay = 1000
-  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
+  exclude_dir = ["assets", "tmp", "vendor", "testdata", "ui"]
   exclude_file = []
   exclude_regex = ["_test.go"]
   exclude_unchanged = false
diff --git a/lib/tasks.go b/lib/tasks.go
index bebafae..bf71a0d 100644
--- a/lib/tasks.go
+++ b/lib/tasks.go
@@ -1,6 +1,7 @@
 package lib
 
 import (
+	"fmt"
 	"time"
 
 	"rul.sh/go-ytmp3/utils"
@@ -75,6 +76,10 @@ func InitTaskScheduler() *TaskScheduler {
 					Album:     task.Album,
 				})
 
+				if err != nil {
+					fmt.Println(err)
+				}
+
 				task.IsPending = false
 				task.Error = err
 				task.Result = result
diff --git a/lib/yt2mp3.go b/lib/yt2mp3.go
index 91a9f59..0133671 100644
--- a/lib/yt2mp3.go
+++ b/lib/yt2mp3.go
@@ -18,8 +18,10 @@ import (
 	"rul.sh/go-ytmp3/utils"
 )
 
-func fetchVideo(video *goutubedl.Result, out string, ch chan error) {
-	dl, err := video.Download(context.Background(), "best")
+func fetchAudio(video *goutubedl.Result, out string, ch chan error) {
+	dl, err := video.DownloadWithOptions(context.Background(), goutubedl.DownloadOptions{
+		DownloadAudioOnly: true,
+	})
 	if err != nil {
 		ch <- err
 		return
@@ -167,7 +169,7 @@ func Yt2Mp3(video *goutubedl.Result, options Yt2Mp3Options) (string, error) {
 		videoSlug = slug.Make(title)
 	}
 
-	videoSrc := fmt.Sprintf("%s/%s.mp4", tmpDir, videoSlug)
+	audioSrc := fmt.Sprintf("%s/%s-src.mp3", tmpDir, videoSlug)
 	thumbnail := fmt.Sprintf("%s/%s.jpg", tmpDir, videoSlug)
 	out := fmt.Sprintf("%s/%s.mp3", options.OutDir, videoSlug)
 
@@ -178,7 +180,7 @@ func Yt2Mp3(video *goutubedl.Result, options Yt2Mp3Options) (string, error) {
 	videoCh := make(chan error)
 	thumbCh := make(chan error)
 
-	go fetchVideo(video, videoSrc, videoCh)
+	go fetchAudio(video, audioSrc, videoCh)
 	go fetchThumbnail(video, thumbnail, thumbCh)
 
 	err := <-videoCh
@@ -196,7 +198,7 @@ func Yt2Mp3(video *goutubedl.Result, options Yt2Mp3Options) (string, error) {
 	convertCh := make(chan error)
 
 	go convertToMp3(ConvertOptions{
-		Video:     videoSrc,
+		Video:     audioSrc,
 		Thumbnail: thumbnail,
 		Title:     title,
 		Artist:    artist,