From: Mike Lowis Date: Mon, 1 Apr 2024 17:05:48 +0000 (-0400) Subject: switched sources over ro URIs, added an indexing script, and updated player to use... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=3f8286c84de39287545f6ae3693f1e38b9d81a4b;p=proto%2Fatv.git switched sources over ro URIs, added an indexing script, and updated player to use video URIs as provided --- diff --git a/atv/assets/index.html b/atv/assets/index.html index 01f261b..b7315b5 100644 --- a/atv/assets/index.html +++ b/atv/assets/index.html @@ -67,8 +67,7 @@ const updatePlayer = (prev)=>{ // Check if we need to refresh the URI const diffTooBig = Math.abs(Math.floor(Video.currentTime) - elapsed) > 3; - const srcChanged = Video.src.replace(/#t=[0-9]+$/, '') != encodeURI(document.location + current["curr"]["path"]); - + const srcChanged = Video.src.replace(/#t=[0-9]+$/, '') != current["curr"]["path"]; const playStateChanged = prev["playing"] != current["playing"]; const chanChanged = prev["chan"] != current["chan"]; diff --git a/atv/bin/atv-index b/atv/bin/atv-index index 094ced7..1970ebe 100644 --- a/atv/bin/atv-index +++ b/atv/bin/atv-index @@ -1,7 +1,27 @@ #!/bin/env ruby +require 'json' + TYPES = "{mp4,ogg,webm}" -FILES = ARGV.map {|f| Dir.glob("#{f}/**/*.#{TYPES}") }.flatten -FILES.each do |f| -end +FILES = [] +CMD="ffprobe -show_entries format=duration -v quiet -of csv=\"p=0\" -i" +ARGV.each do |root| + # Generate the index + index = [] + Dir.glob("#{root}/**/*.#{TYPES}").each do |path| + short_path = path.sub("#{root}/", '') + puts short_path + duration = `#{CMD} \"#{path}\"`.chomp.to_f + entry = { + "path" => short_path, + "duration" => duration + } + index << entry + end + + # Save the index to disc + File.open("#{root}/index.json", "wb") do |f| + f.write JSON.dump(index) + end +end diff --git a/atv/lib/atv/database.rb b/atv/lib/atv/database.rb index 829376c..d96bfee 100644 --- a/atv/lib/atv/database.rb +++ b/atv/lib/atv/database.rb @@ -1,36 +1,36 @@ require 'json' +require 'open-uri' module ATV class Database CMD="ffprobe -show_entries format=duration -v quiet -of csv=\"p=0\" -i" - def initialize(root, scan_dirs = []) + def initialize(root, sources = []) @root = root @path = "#{root}/index.json" @data = {} - @dirs = scan_dirs - pp scan_dirs + @cache = {} + @sources = sources load() end def load() - @cache = {} - if File.exist? @path - @data = JSON.parse(File.read(@path)) + @sources.each do |source| + uri = "#{source}/index.json" + begin + index = JSON.parse(URI.open(uri).read) + index.each do |item| + short_path = item["path"] + item["path"] = URI::Parser.new.escape("#{source}/#{short_path}") + @data[short_path] = item + end + rescue Exception => e + puts "Failed to open URI #{uri}: #{e}" + end end - cleanup() - scan() - save() pp @data end - def save() - File.open(@path, "wb") do |f| - puts f - f.write JSON.dump(@data) - end - end - def files @data.keys.map do |f| @data[f].merge({ "path" => f }) @@ -41,35 +41,13 @@ module ATV @data[key] end - def cleanup() - files.each do |f| - if not File.exist?("#{ATV_ROOT}/#{f["path"]}") - puts "Cleanup #{f["path"]}" - @data.delete(f["path"]) - end - end - end - - def scan() - # scan for videos - @dirs.each do |dir| - Dir.glob("#{@root}/#{dir}/**/*.{mp4,m4v,webm,ogg}").each do |f| - short_path = f.sub("#{@root}/", '') - next if @data[short_path] - puts f - duration = `#{CMD} \"#{f}\"`.chomp.to_f - @data[short_path] = { "duration" => duration } - end - end - end - def select_files(channel, selectors) if @cache[channel].nil? then @cache[channel] = { name: channel, items: {} } tree = {} @cache[channel][:items] = selectors.map do |sel| - @data.keys.select {|f| f.sub(%r{[^/]+/}, '').start_with? sel } + @data.keys.select {|f| f.start_with? sel } end.flatten.uniq.map do |e| # Add item to the virtual directory tree node = tree @@ -79,15 +57,16 @@ module ATV node = node[folder] end - obj = { "path" => e, "duration" => @data[e]["duration"]} - node[path.last] = obj + node[path.last] = @data[e] # Return the key/value pair - [e, obj] + [e, @data[e]] end.to_h # save off the tree @cache[channel][:tree] = tree + pp @cache[channel] + end @cache[channel] end