all repos — gopipe @ 2c659855b1c3bb20d334660e1f0c2aee94652db3

Embed YouTube videos on Telegram, Discord and more!

better format selector
Marco Andronaco andronacomarco@gmail.com
Fri, 18 Oct 2024 16:33:36 +0200
commit

2c659855b1c3bb20d334660e1f0c2aee94652db3

parent

72f5236b15f0ae2fd00b01b36a7bce52dbd2c429

4 files changed, 43 insertions(+), 33 deletions(-)

jump to
M src/app/handlers.gosrc/app/handlers.go

@@ -95,17 +95,18 @@ videoURL = fmt.Sprintf("/proxy/%s/%d", videoID, formatID)

} data := map[string]interface{}{ - "VideoID": videoID, - "VideoURL": videoURL, - "Author": video.Author, - "Title": video.Title, - "Description": video.Description, - "Thumbnail": thumbnail, - "Duration": video.Duration, - "Captions": getCaptions(*video), - "Heading": template.HTML(heading), - "VideoFormats": video.Formats.Select(formatsSelectFnVideo), - "AudioFormats": video.Formats.Select(formatsSelectFnAudio), + "VideoID": videoID, + "VideoURL": videoURL, + "Author": video.Author, + "Title": video.Title, + "Description": video.Description, + "Thumbnail": thumbnail, + "Duration": video.Duration, + "Captions": getCaptions(*video), + "Heading": template.HTML(heading), + "AudioVideoFormats": video.Formats.Select(formatsSelectFnAudioVideo), + "VideoFormats": video.Formats.Select(formatsSelectFnVideo), + "AudioFormats": video.Formats.Select(formatsSelectFnAudio), } err = g.XT.ExecuteTemplate(w, "video.tmpl", data)
M src/app/video.gosrc/app/video.go

@@ -65,19 +65,23 @@ return c

} func formatsSelectFn(f youtube.Format) bool { - return f.AudioChannels > 1 && f.ContentLength < maxContentLength && strings.HasPrefix(f.MimeType, "video/mp4") + return f.AudioChannels > 0 && f.ContentLength < maxContentLength && strings.HasPrefix(f.MimeType, "video/mp4") } func formatsSelectFnBest(f youtube.Format) bool { - return f.AudioChannels > 1 && strings.HasPrefix(f.MimeType, "video/mp4") + return f.AudioChannels > 0 && strings.HasPrefix(f.MimeType, "video/mp4") } -func formatsSelectFnAudio(f youtube.Format) bool { - return f.QualityLabel == "" +func formatsSelectFnAudioVideo(f youtube.Format) bool { + return f.AudioChannels > 0 && f.QualityLabel != "" } func formatsSelectFnVideo(f youtube.Format) bool { - return !formatsSelectFnAudio(f) + return f.QualityLabel != "" && f.AudioChannels == 0 +} + +func formatsSelectFnAudio(f youtube.Format) bool { + return f.QualityLabel == "" } func getURL(videoID string) string {
M src/globals/globals.gosrc/globals/globals.go

@@ -50,14 +50,8 @@ res += " - " + mime[0]

codecs := " (" + strings.Split(mime[1], "\"")[1] + ")" - if isAudio { - return res + " - audio only" + codecs - } - - res += fmt.Sprintf(" (%d FPS)", f.FPS) - - if f.AudioChannels == 0 { - res += " - video only" + if !isAudio { + res += fmt.Sprintf(" (%d FPS)", f.FPS) } res += codecs
M templates/video.tmpltemplates/video.tmpl

@@ -36,16 +36,27 @@ <pre style="white-space: pre-wrap">{{ .Description }}</pre>

<form action="/download" method="post" rel="noopener" target="_blank" style="display: grid; grid-template-columns: auto auto; justify-content: space-between;"> <input type="hidden" name="video" value="{{ .VideoID }}"> <select name="itagno"> + <optgroup label="Audio & Video"> + {{ range .AudioVideoFormats }} + <option value="{{ .ItagNo }}"> + {{ parseFormat . }} + </option> + {{ end }} + </optgroup> + <optgroup label="Video only"> {{ range .VideoFormats }} - <option value="{{ .ItagNo }}"> - {{ parseFormat . }} - </option> - {{ end }} - {{ range .AudioFormats }} - <option value="{{ .ItagNo }}"> - {{ parseFormat . }} - </option> - {{ end }} + <option value="{{ .ItagNo }}"> + {{ parseFormat . }} + </option> + {{ end }} + </optgroup> + <optgroup label="Audio only"> + {{ range .AudioFormats }} + <option value="{{ .ItagNo }}"> + {{ parseFormat . }} + </option> + {{ end }} + </optgroup> </select> <button type="submit">Download</button> </form>