From e1391f4ac7ba55ef514c1ff67eea0b577b24f290 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Tue, 31 Jul 2018 13:19:38 -0500 Subject: [PATCH 1/2] Use videojs-markers for start end end times --- src/invidious.cr | 20 ++++++++++- src/invidious/helpers.cr | 18 +++++----- src/invidious/views/embed.ecr | 65 +++++++++++++++++++++++++---------- src/invidious/views/watch.ecr | 29 +++++++++++++--- 4 files changed, 101 insertions(+), 31 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 7e181bd9..778556ba 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -287,7 +287,7 @@ get "/watch" do |env| subscriptions ||= [] of String autoplay = env.params.query["autoplay"]?.try &.to_i - video_loop = env.params.query["video_loop"]?.try &.to_i + video_loop = env.params.query["loop"]?.try &.to_i if preferences autoplay ||= preferences.autoplay.to_unsafe @@ -1287,6 +1287,10 @@ get "/embed/:id" do |env| video_loop = env.params.query["loop"]?.try &.to_i video_loop ||= 0 + autoplay = autoplay == 1 + video_loop = video_loop == 1 + controls = controls == 1 + begin video = get_video(id, PG_DB) rescue ex @@ -1294,6 +1298,20 @@ get "/embed/:id" do |env| next templated "error" end + if video.info["hlsvp"]? + hlsvp = video.info["hlsvp"] + + if Kemal.config.ssl || CONFIG.https_only + scheme = "https://" + else + scheme = "http://" + end + host = env.request.headers["Host"] + url = "#{scheme}#{host}" + + hlsvp = hlsvp.gsub("https://manifest.googlevideo.com", url) + end + fmt_stream = [] of HTTP::Params video.info["url_encoded_fmt_stream_map"].split(",") do |string| if !string.empty? diff --git a/src/invidious/helpers.cr b/src/invidious/helpers.cr index 40eeb52f..3cae652e 100644 --- a/src/invidious/helpers.cr +++ b/src/invidious/helpers.cr @@ -891,16 +891,16 @@ def decode_time(string) time = string.try &.to_f? if !time - hours = /(?\d+)h/.match(string).try &.["hours"].try &.to_i + hours = /(?\d+)h/.match(string).try &.["hours"].try &.to_f hours ||= 0 - minutes = /(?\d+)m(?!s)/.match(string).try &.["minutes"].try &.to_i + minutes = /(?\d+)m(?!s)/.match(string).try &.["minutes"].try &.to_f minutes ||= 0 - seconds = /(?\d+)s/.match(string).try &.["seconds"].try &.to_i + seconds = /(?\d+)s/.match(string).try &.["seconds"].try &.to_f seconds ||= 0 - millis = /(?\d+)ms/.match(string).try &.["millis"].try &.to_i + millis = /(?\d+)ms/.match(string).try &.["millis"].try &.to_f millis ||= 0 time = hours * 3600 + minutes * 60 + seconds + millis / 1000 @@ -909,10 +909,12 @@ def decode_time(string) return time end -def decode_date(date : String) +def decode_date(string : String) # Time matches format "20 hours ago", "40 minutes ago"... - delta = date.split(" ")[0].to_i - case date + date = string.split(" ")[-3, 3] + delta = date[0].to_i + + case date[1] when .includes? "minute" delta = delta.minutes when .includes? "hour" @@ -926,7 +928,7 @@ def decode_date(date : String) when .includes? "year" delta = delta.years else - raise "Could not parse #{date}" + raise "Could not parse #{string}" end return Time.now - delta diff --git a/src/invidious/views/embed.ecr b/src/invidious/views/embed.ecr index e8df67fe..82e5070a 100644 --- a/src/invidious/views/embed.ecr +++ b/src/invidious/views/embed.ecr @@ -7,10 +7,11 @@ + - + <%= video.title %> - Invidious @@ -29,23 +30,31 @@ video, #my_video, .video-js, .vjs-default-skin } +<% if hlsvp %> + +<% end %> + @@ -59,18 +68,19 @@ var options = { 'volumePanel', 'progressControl', 'remainingTimeDisplay', + 'captionsButton', 'qualitySelector', 'playbackRateMenuButton', 'fullscreenToggle', ], }, }; + var player = videojs('player', options, function() { this.hotkeys({ volumeStep: 0.1, seekStep: 5, enableModifiersForNumbers: false, - enableVolumeScroll: false, customKeys: { play: { key: function(e) { @@ -107,11 +117,30 @@ var player = videojs('player', options, function() { }); }); -player.offset({ - start: <%= video_start %>, - end: <%= video_end %> +<% if video_start > 0 || video_end > 0 %> +player.markers({ + onMarkerReached: function(marker) { + if (marker.text === 'End') { + if (player.loop()) { + player.markers.prev('Start'); + } else { + player.pause(); + } + } + }, + markers: [ + {time: <%= video_start %>, text: 'Start'}, + <% if video_end < 0 %> + {time: <%= video.info["length_seconds"].to_f - 0.5 %>, text: 'End'} + <% else %> + {time: <%= video_end %>, text: 'End'} + <% end %> + ] }); +player.currentTime(<%= video_start %>); +<% end %> + <% if !listen %> var currentSources = player.currentSources(); for ( var i = 0; i < currentSources.length; i++ ) { diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index dfb94f9b..8a9ef978 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -24,10 +24,11 @@ + - + <%= video.title %> - Invidious <% end %> @@ -130,11 +131,31 @@ var player = videojs('player', options, function() { player.volume(<%= preferences.volume.to_f / 100 %>); player.playbackRate(<%= preferences.speed %>); <% end %> -player.offset({ - start: <%= video_start %>, - end: <%= video_end %> + +<% if video_start > 0 || video_end > 0 %> +player.markers({ + onMarkerReached: function(marker) { + if (marker.text === 'End') { + if (player.loop()) { + player.markers.prev('Start'); + } else { + player.pause(); + } + } + }, + markers: [ + {time: <%= video_start %>, text: 'Start'}, + <% if video_end < 0 %> + {time: <%= video.info["length_seconds"].to_f - 0.5 %>, text: 'End'} + <% else %> + {time: <%= video_end %>, text: 'End'} + <% end %> + ] }); +player.currentTime(<%= video_start %>); +<% end %> + <% if !listen %> var currentSources = player.currentSources(); for ( var i = 0; i < currentSources.length; i++ ) { From b5bbf1b9f2f724601a7c55dfe2acac4ac9ab93ba Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Tue, 31 Jul 2018 13:40:26 -0500 Subject: [PATCH 2/2] Add share button --- src/invidious.cr | 32 +++++++++++++++++++++++++++++++- src/invidious/views/embed.ecr | 13 +++++++++++++ src/invidious/views/watch.ecr | 13 +++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/invidious.cr b/src/invidious.cr index 778556ba..4b18f681 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -430,6 +430,16 @@ get "/watch" do |env| description = " " end + if Kemal.config.ssl || CONFIG.https_only + scheme = "https://" + else + scheme = "http://" + end + host = env.request.headers["Host"] + host_url = "#{scheme}#{host}" + host_params = env.request.query_params + host_params.delete_all("v") + thumbnail = "https://i.ytimg.com/vi/#{id}/mqdefault.jpg" templated "watch" @@ -1357,6 +1367,26 @@ get "/embed/:id" do |env| next env.redirect url end + video.description = fill_links(video.description, "https", "www.youtube.com") + video.description = add_alt_links(video.description) + + description = video.description.gsub("
", " ") + description = description.gsub("
", " ") + description = XML.parse_html(description).content[0..200].gsub('"', """).gsub("\n", " ").strip(" ") + if description.empty? + description = " " + end + + if Kemal.config.ssl || CONFIG.https_only + scheme = "https://" + else + scheme = "http://" + end + host = env.request.headers["Host"] + host_url = "#{scheme}#{host}" + host_params = env.request.query_params + host_params.delete_all("v") + thumbnail = "https://i.ytimg.com/vi/#{id}/mqdefault.jpg" rendered "embed" @@ -2810,7 +2840,7 @@ get "/:id" do |env| params = params.join("&") url = "/watch?v=#{id}" - if params + if !params.empty? url += "&#{params}" end diff --git a/src/invidious/views/embed.ecr b/src/invidious/views/embed.ecr index 82e5070a..f3613247 100644 --- a/src/invidious/views/embed.ecr +++ b/src/invidious/views/embed.ecr @@ -8,10 +8,12 @@ + + <%= video.title %> - Invidious @@ -76,6 +78,15 @@ var options = { }, }; +var shareOptions = { + socials: ["fb", "tw", "reddit", "mail"], + + url: "<%= host_url %>/<%= video.id %>?<%= host_params %>", + title: "<%= video.title %>", + description: "<%= description %>", + image: '<%= thumbnail %>' +}; + var player = videojs('player', options, function() { this.hotkeys({ volumeStep: 0.1, @@ -117,6 +128,8 @@ var player = videojs('player', options, function() { }); }); +player.share(shareOptions); + <% if video_start > 0 || video_end > 0 %> player.markers({ onMarkerReached: function(marker) { diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index 8a9ef978..6cbcb19f 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -25,10 +25,12 @@ + + <%= video.title %> - Invidious <% end %> @@ -86,6 +88,15 @@ var options = { }, }; +var shareOptions = { + socials: ["fb", "tw", "reddit", "mail"], + + url: "<%= host_url %>/<%= video.id %>?<%= host_params %>", + title: "<%= video.title %>", + description: "<%= description %>", + image: '<%= thumbnail %>' +}; + var player = videojs('player', options, function() { this.hotkeys({ volumeStep: 0.1, @@ -127,6 +138,8 @@ var player = videojs('player', options, function() { }); }); +player.share(shareOptions); + <% if preferences %> player.volume(<%= preferences.volume.to_f / 100 %>); player.playbackRate(<%= preferences.speed %>);