@@ -459,3 +459,64 @@ func CancelFormats(fm *FormatsManager) tea.Cmd {
459459 return types.CancelFormatsMsg {}
460460 })
461461}
462+
463+ func FetchVideoInfo (fm * FormatsManager , url string ) tea.Cmd {
464+ return tea .Cmd (func () tea.Msg {
465+ cfg , err := config .Load ()
466+ if err != nil {
467+ log .Printf ("Warning: Failed to load config, using defaults: %v" , err )
468+ cfg = config .GetDefault ()
469+ }
470+
471+ ytDlpPath := cfg .YTDLPPath
472+ if ytDlpPath == "" {
473+ ytDlpPath = "yt-dlp"
474+ }
475+
476+ cmd := exec .Command (ytDlpPath , "-J" , url )
477+
478+ fm .SetCmd (cmd )
479+
480+ stdout , err := cmd .StdoutPipe ()
481+ if err != nil {
482+ return types.PlayURLResultMsg {URL : url , Err : fmt .Sprintf ("Failed to get video info: %v" , err )}
483+ }
484+
485+ if err := cmd .Start (); err != nil {
486+ fm .Clear ()
487+ return types.PlayURLResultMsg {URL : url , Err : fmt .Sprintf ("Failed to start yt-dlp: %v" , err )}
488+ }
489+
490+ out , err := io .ReadAll (stdout )
491+ if closeErr := stdout .Close (); closeErr != nil {
492+ log .Printf ("failed to close video info stdout: %v" , closeErr )
493+ }
494+
495+ if fm .ClearAndCheckCanceled () {
496+ return types.PlayURLResultMsg {URL : url , Err : "Canceled" }
497+ }
498+
499+ if err != nil {
500+ return types.PlayURLResultMsg {URL : url , Err : fmt .Sprintf ("Failed to read video info: %v" , err )}
501+ }
502+
503+ if len (out ) == 0 {
504+ return types.PlayURLResultMsg {URL : url , Err : "No video info found" }
505+ }
506+
507+ var data map [string ]any
508+ if err := json .Unmarshal (out , & data ); err != nil {
509+ return types.PlayURLResultMsg {URL : url , Err : fmt .Sprintf ("Failed to parse video info: %v" , err )}
510+ }
511+
512+ videoInfo := extractVideoInfo (data )
513+ if videoInfo .ID == "" {
514+ return types.PlayURLResultMsg {URL : url , Err : "Could not extract video ID from URL" }
515+ }
516+
517+ return types.PlayURLResultMsg {
518+ URL : url ,
519+ SelectedVideo : videoInfo ,
520+ }
521+ })
522+ }
0 commit comments