Invidious is an alternative front-end to YouTube
Use videojs-markers for start end end times
Omar Rothe1391f4ac7ba
diff --git a/src/invidious.cr b/src/invidious.crindex 7e181bd9..778556ba 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -287,7 +287,7 @@ get "/watch" do |env|subscriptions ||= [] of Stringautoplay = 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_iif preferencesautoplay ||= preferences.autoplay.to_unsafe@@ -1287,6 +1287,10 @@ get "/embed/:id" do |env|video_loop = env.params.query["loop"]?.try &.to_ivideo_loop ||= 0+ autoplay = autoplay == 1+ video_loop = video_loop == 1+ controls = controls == 1+beginvideo = 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::Paramsvideo.info["url_encoded_fmt_stream_map"].split(",") do |string|if !string.empty?diff --git a/src/invidious/helpers.cr b/src/invidious/helpers.crindex 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 = /(?<hours>\d+)h/.match(string).try &.["hours"].try &.to_i+ hours = /(?<hours>\d+)h/.match(string).try &.["hours"].try &.to_fhours ||= 0- minutes = /(?<minutes>\d+)m(?!s)/.match(string).try &.["minutes"].try &.to_i+ minutes = /(?<minutes>\d+)m(?!s)/.match(string).try &.["minutes"].try &.to_fminutes ||= 0- seconds = /(?<seconds>\d+)s/.match(string).try &.["seconds"].try &.to_i+ seconds = /(?<seconds>\d+)s/.match(string).try &.["seconds"].try &.to_fseconds ||= 0- millis = /(?<millis>\d+)ms/.match(string).try &.["millis"].try &.to_i+ millis = /(?<millis>\d+)ms/.match(string).try &.["millis"].try &.to_fmillis ||= 0time = hours * 3600 + minutes * 60 + seconds + millis / 1000@@ -909,10 +909,12 @@ def decode_time(string)return timeend-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.minuteswhen .includes? "hour"@@ -926,7 +928,7 @@ def decode_date(date : String)when .includes? "year"delta = delta.yearselse- raise "Could not parse #{date}"+ raise "Could not parse #{string}"endreturn Time.now - deltadiff --git a/src/invidious/views/embed.ecr b/src/invidious/views/embed.ecrindex e8df67fe..82e5070a 100644--- a/src/invidious/views/embed.ecr+++ b/src/invidious/views/embed.ecr@@ -7,10 +7,11 @@<meta name="thumbnail" content="<%= thumbnail %>"><link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/video-js.min.css"><link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/css/quality-selector.css">+<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/videojs.markers.min.css"><script src="https://unpkg.com/[email protected]/dist/video.min.js"></script><script src="https://unpkg.com/[email protected]/videojs.hotkeys.min.js"></script><script src="https://unpkg.com/[email protected]/dist/js/silvermine-videojs-quality-selector.min.js"></script>-<script src="https://unpkg.com/[email protected]/dist/videojs-offset.min.js"></script>+<script src="https://unpkg.com/[email protected]/dist/videojs-markers.min.js"></script><title><%= video.title %> - Invidious</title></head>@@ -29,23 +30,31 @@ video, #my_video, .video-js, .vjs-default-skin}</style>+<% if hlsvp %>+<script src="https://unpkg.com/[email protected]/dist/videojs-contrib-hls.min.js"></script>+<% end %>+<video playsinline poster="<%= thumbnail %>" title="<%= HTML.escape(video.title) %>" id="player"- <% if autoplay == 1 %>autoplay<% end %>- <% if controls == 1 %>controls<% end %>- <% if video_loop == 1 %>loop<% end %>+ <% if autoplay %>autoplay<% end %>+ <% if controls %>controls<% end %>+ <% if video_loop %>loop<% end %>class="video-js vjs-default-skin">- <% if listen %>- <% audio_streams.each_with_index do |fmt, i| %>- <source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["bitrate"] %>k" selected="<%= i == 0 ? true : false %>">- <% end %>+ <% if hlsvp %>+ <source src="<%= hlsvp %>" type="application/x-mpegURL"><% else %>- <% fmt_stream.each_with_index do |fmt, i| %>- <% if quality != "hd720" %>- <source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= quality == fmt["label"].split(" - ")[0] %>">- <% else %>- <source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= i == 0 ? true : false %>">+ <% if listen %>+ <% audio_streams.each_with_index do |fmt, i| %>+ <source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["bitrate"] %>k" selected="<%= i == 0 ? true : false %>"><% end %>- <% end %>+ <% else %>+ <% fmt_stream.each_with_index do |fmt, i| %>+ <% if quality != "hd720" %>+ <source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= quality == fmt["label"].split(" - ")[0] %>">+ <% else %>+ <source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= i == 0 ? true : false %>">+ <% end %>+ <% end %>+ <% end %><% end %></video>@@ -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.ecrindex dfb94f9b..8a9ef978 100644--- a/src/invidious/views/watch.ecr+++ b/src/invidious/views/watch.ecr@@ -24,10 +24,11 @@<meta name="twitter:player:height" content="720"><link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/video-js.min.css"><link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/css/quality-selector.css">+<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/videojs.markers.min.css"><script src="https://unpkg.com/[email protected]/dist/video.min.js"></script><script src="https://unpkg.com/[email protected]/videojs.hotkeys.min.js"></script><script src="https://unpkg.com/[email protected]/dist/js/silvermine-videojs-quality-selector.min.js"></script>-<script src="https://unpkg.com/[email protected]/dist/videojs-offset.min.js"></script>+<script src="https://unpkg.com/[email protected]/dist/videojs-markers.min.js"></script><title><%= video.title %> - Invidious</title><% 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++ ) {