Youtube video title and duration without api key - youtube

How can get the youtube video title and duration without using api key ?
I have checked Youtube Video title with API v3 without API key? link but this only give the title and not the duration.
So how can I get duration also without api key ?

To people looking for an answer to this in 2020. You can do this using the Youtube Iframe API. You don't need a API key!
First you insert the api link into the JS like so:
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
After that you create the player with the desired videoID like this:
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'SIWbjgPYcJY',
playerVars: {
'autoplay': 0,
'controls': 1
},
events: {
'onReady': onPlayerReady
}
});
}
Then you need to get the title and duration by assigning these in a variable to your HTML. We place this in the onPlayerReady.
function onPlayerReady(event) {
time = player.getDuration();
$(".dur").val(time);
title = player.getVideoData().title
$(".title").val(title);
}
Last thing is to put the player, duration and title in the HTML:
<div id="player"></div>
<br>
<span>Titel:</span><input class="title">
<br>
<span>duration:</span><input class="dur">
<br>
JSFIDDLE

You can use youtube-dl:
$ youtube-dl -e "https://www.youtube.com/watch?v=2i2khp_npdE"
Alan Walker - Sing Me To Sleep
$ youtube-dl --get-duration "https://www.youtube.com/watch?v=2i2khp_npdE"
3:12
Alternatively:
$ curl -s "https://www.youtube.com/watch?v=2i2khp_npdE" | tr '<' '\n' | awk -F'"' '/name="title"/ { print $4 }'
Alan Walker - Sing Me To Sleep
$ curl -s "https://www.youtube.com/watch?v=2i2khp_npdE" | awk -F'"' '/itemprop="duration"/ { print $4 }'
PT3M12S
You can use this bash function to convert from the Youtube format to seconds:
duration_calculation ()
{
local total_in_second=0
local hours=0
local minutes=0
local seconds=0
local duration="$1"
if [[ $duration =~ ^PT([0-9]{1,})H([0-9]{1,})M([0-9]{1,})S$ ]]; then
hours=$((hours + BASH_REMATCH[1]))
minutes=$((minutes + BASH_REMATCH[2]))
seconds=$((seconds + BASH_REMATCH[3]))
# PT1H4M H:M:00
elif [[ $duration =~ ^PT([0-9]{1,})H([0-9]{1,})M$ ]];then
hours=$((hours + BASH_REMATCH[1]))
minutes=$((minutes + BASH_REMATCH[2]))
# PT1H29S H:00:S
elif [[ $duration =~ ^PT([0-9]{1,})H([0-9]{1,})S$ ]]; then
hours=$((hours + BASH_REMATCH[1]))
seconds=$((seconds + BASH_REMATCH[2]))
# PT4M29S M:S
elif [[ $duration =~ ^PT([0-9]{1,})M([0-9]{1,})S$ ]]; then
minutes=$((minutes + BASH_REMATCH[1]))
seconds=$((seconds + BASH_REMATCH[2]))
# PT1H H:00:00
elif [[ $duration =~ ^PT([0-9]{1,})H$ ]]; then
hours=$((hours + BASH_REMATCH[1]))
# PT4M 00:M:00
elif [[ $duration =~ ^PT([0-9]{1,})M$ ]]; then
minutes=$((minutes + BASH_REMATCH[1]))
# PT29S S
elif [[ $duration =~ ^PT([0-9]{1,})S$ ]]; then
seconds=$((seconds + BASH_REMATCH[1]))
fi
total_in_seconds=$(( (hours * 3600) + (minutes * 60) + seconds ))
echo "$total_in_seconds"
}

Related

Automatically align markdown tables using lua in Neovim

I am using vim-easy-align plugin and full lua configuration for my Neovim
This is how we can auto align the markdown tables for every '|' we type using vim-tabular in VimScript
inoremap <silent> <Bar> <Bar><Esc>:call <SID>align()<CR>a
function! s:align()
let p = '^\s*|\s.*\s|\s*$'
if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
Tabularize/|/l1
normal! 0
call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
endif
endfunction
Source: Tim Pope's Gist
I want something like the above but for vim-easy-align and in Lua
This is the best I could do
local set_cursor_to_nth_bar = function (row, count)
local line = vim.api.nvim_buf_get_lines(0, row, row + 1, false)[1]
local cur_bar_count = 0
local cur_col = 0 -- will be the col of the cursor + 1 by the end of the loop
while cur_bar_count < count do
cur_col = line:find('|', cur_col + 1)
cur_bar_count = cur_bar_count + 1
end
vim.api.nvim_win_set_cursor(0, {row + 1, cur_col})
vim.cmd [[startinsert!]]
end
local on_bar_inserted = function ()
local pos = vim.api.nvim_win_get_cursor(0)
local row = pos[1] - 1
local col = pos[2]
local before_line = vim.api.nvim_get_current_line()
-- record the number of bars in the line prior to the cursor
local _, pre_bar_count = before_line:sub(0, col):gsub("|", "|")
-- insert the |
vim.api.nvim_buf_set_text(0, row, col, row, col, { '|' })
-- Easy Align markdown table
vim.cmd [[ stopinsert ]]
vim.cmd [[ normal! vip ]] -- visually select the paragraph
vim.api.nvim_feedkeys('ga*|', 'v', false) -- EasyAlign all |s in the paragraph -- ga = keymap for <Plug>(EasyAlign)
-- place the cursor at the end of the entered | (pre_bar_count + 1)
-- we need to schedule this since the above nvim_feedkeys need to trigger EasyAlign and it needs to
-- update text before we try to set the cursor in the right place
vim.schedule(
function ()
set_cursor_to_nth_bar(row, pre_bar_count + 1)
end
)
end
-- set ga as keymap for EasyAlign in normal and visual models
vim.keymap.set('n', 'ga', '<Plug>(EasyAlign)', { desc = "Align", noremap = false })
vim.keymap.set('x', 'ga', '<Plug>(EasyAlign)', { desc = "Align", noremap = false })
local align_group = vim.api.nvim_create_augroup('AlignMDTable', { clear = true })
vim.api.nvim_create_autocmd('FileType', {
callback = function()
vim.keymap.set('i', '<Bar>', on_bar_inserted, { desc = "Align Tables", silent = true })
end,
group = align_group,
pattern = "markdown",
})
I think this could be improved. Especially this part, where we are having to use nvim_feedkeys and ga the keymap for <Plug>(EasyAlign)
-- Easy Align markdown table
vim.cmd [[ stopinsert ]]
vim.cmd [[ normal! vip ]] -- visually select the paragraph
vim.api.nvim_feedkeys('ga*|', 'v', false) -- EasyAlign all |s in the paragraph -- ga = keymap for <Plug>(EasyAlign)

Rangeslider ACF in Foreach loop

I have a range slider in my Advanced custom fields. I want the number to be the amount of list items that it echo's out. In example, i put the range slider on 4 in ACF then it needs to echo out:
Flywheel 1
Flywheel 2
Flywheel 3
Flywheel 4
This is the code i have but it's not working, can someone please fix the code?
<?php
$flywheelnumber = get_field('flywheels');
if( $flywheelnumber ) {
echo '<form class="checklist-list">';
echo '<ul>';
foreach($flywheelnumber) {
echo '<li>';
echo ( $numbers );
echo '</li>';
}
echo '</ul>';
echo '</form>'
}
?>
Thanks!!
you have to use for loop. you can try these codes,
<?php
$flywheelnumber = get_field( 'flywheels' );
if ( $flywheelnumber ) {
echo '<form class="checklist-list">';
echo '<ul>';
for ( $x = 1; $x <= count( $flywheelnumber ); $x ++ ) {
echo '<li>' . $x . '</li>';
}
echo '</ul>';
echo '</form>';
}
?>

Rails lazy_high_charts: how to format tooltip label

I would to print into my charts the hours and minutes, for example "development: 27 h 30 m".
Now I have this:
In my script "data" represents "hours"
{
name: value.blank? ? "other" : value,
data: all_weeks.merge(data_weeks.to_h).values.map do |data_value|
data_value.nil? ? 0 : ( data_value.to_f / 60 ).round(2)
end
}
....
f.tooltip(
pointFormat: "<span style='color:{series.color}'>{series.name}</span>: <b>{point.y} h</b><br/>",
split: true
)
I have tried to set point.y into pointFormat but no have effects. ( Edit: pointFormat would a generic string! )
How can I solve? Thanks in advance.
Edit:
I solved adding:
LazyHighCharts::HighChart.new("container") do |f|
f.tooltip(
formatter: "\
function() { \
var s = []; \
s.push(this.x); \
this.points.forEach(function(point) { \
s.push('<b>' + point.series.name + '</b>: ' + Math.floor(point.y) + ' h ' + Math.floor((point.y % 1) * 60) + ' m'); \
}); \
return s; \
}".js_code,
split: true
)
....
end
Maybe you could introduce two variables, data_hours and data_minutes, and write a string interpolation with theses variables.
data_hours = data_value.to_i
data_minutes = ((data_value - data_hours) * 60).to_i
data_to_display = "#{data_hours} h #{data_minutes} m"
Hope it will help !
Written in 0.12 hours ;)
["dev: 27.5 h", "dev: 22 h", "dev: 0.3h"].map do |s|
s.gsub(/\d+(\.\d+)?\s*h/i) do |h|
h, m = h.to_f.divmod(1)
[
("#{h} h" unless h.zero?),
("#{(60 * m).round} m" unless m.zero?)
].compact.join(" ")
end
end
#⇒ ["dev: 27 h 30 m", "dev: 22 h", "dev: 18 m"]

convert data formatting in a lua file

hello i need to convert 720 data sets from a 1 liner to this format below.
Atm i got them in a open office file with each number in a column but i have no idea how i can convert that formatting.
12 -8906.071289 560.890564 93.236107 0 test2
13 -846.814636 -526.218323 10.981694 0 southshore
to
[12] = {
[1] = "test2",
[2] = "-8906.071289",
[3] = "560.890564",
[4] = "93.236107",
[5] = "0",
},
[13] = {
[1] = "Southshore",
[2] = "-846.814636",
[3] = "-526.218323",
[4] = "10.981694",
[5] = "0",
},
One possibility in Lua. Run with program.lua datafile
where program.lua is whatever name you give this file, and datafile is, well, your external data file. Test with just program.lua
--[[
12 -8906.071289 560.890564 93.236107 0 test2
13 -846.814636 -526.218323 10.981694 0 southshore
--]]
local filename = arg[1] or arg[0] --data from 1st command line argument or this file
local index,head,tail
print '{'
for line in io.lines(filename) do
if line:match '^%d+' then
head, line, tail = line:match '^(%d+)%s+(.-)(%S+)$'
print(' [' .. head .. '] = {\n [1] = "' .. tail .. '",')
index = 1
for line in line:gmatch '%S+' do
index = index + 1
print(' [' .. index .. '] = "' .. line .. '",')
end
print ' },'
end
end
print '}'
This awk program does it:
{
print "[" $1 "] = {"
print "\t[" 1 "] = \"" $NF "\","
for (i=2; i<NF; i++) {
print "\t[" i "] = \"" $i "\","
}
print "},"
}

How to monitor progress from ffmpeg?

I have this ffmpeg video encoder using Rails and I needed to report ffmpeg progress to the user.
How can that be done provided that I am using Linux?
This is the solution I reached:
def execute_ffmpeg(cmd, progress)
logger.debug "Running command #{cmd}"
command = "#{cmd} 2>&1"
progress = nil
frames = nil
fps = 25
ffmpeg = IO.popen(command)
ffmpeg.each("\r") do |line|
if frames.nil? && line =~ /Duration:(\s.?(\d*):(\d*):(\d*\.\d*))/
duration = $2.to_s + ":" + $3.to_s + ":" + $4.to_s
frames = ($4.to_i + ($3.to_i * 60) + ($2.to_i * 60 * 60)) * fps
end
if line =~ /frame=(\s.?(\d*))/
progress = $1.to_i / frames.to_i
print "Progress: #{progress}"
end
end
end
Thanks for the post, I modified it a little. I am using ffmpeg version 0.10.2. Here's my version:
def exec_ffmpeg src_path, dst_path
cmd = "ffmpeg -i \"%s\" -acodec libfaac -ac 2 -ab 128k -vcodec libx264 -threads 0 \"%s\" 2>&1" %
[src_path, dst_path]
puts "%s" % cmd.gsub(/2\>\&1$/,'')
puts "press 'q' to quit"
progress = nil
dur_secs = nil
frame_rate = nil
frames = 0
dur_str = '00:00:00.000'
ostr = ''
ffmpeg = IO.popen(cmd)
ffmpeg.each("\r") do |line|
if dur_secs == nil && line =~ /Duration:\s*(\d*):(\d*):(\d*\.\d*)/
dur_str = $1.to_s + ":" + $2.to_s + ":" + $3.to_s
dur_secs = ($3.to_f + ($2.to_i * 60).to_f + ($1.to_i * 360).to_f)
puts "Video Duration:" + dur_str
end
if frame_rate == nil && line =~ /Stream.+\, (\d+\.{0,1}\d{0,3}) fps\,/
frame_rate = $1.to_f
frames = dur_secs * frame_rate
puts "Total Frames: %i" % frames.to_i
puts "Frame Rate: %.3f fps" % frame_rate
end
if line =~ /frame=(\s.?(\d*))/
cframe = $1.to_i
csecs = 0
if line =~ /time=\s*(\d*):(\d*):(\d*\.\d*)/
csecs = ($3.to_f + ($2.to_i * 60).to_f + ($1.to_i * 360).to_f)
csecs_str = $1.to_s + ":" + $2.to_s + ":" + $3.to_s
elsif line =~ /time=\s*(\d*\.\d*)/
csecs $1.to_f
t = Time.at(csecs).gmtime
csecs_str = "%0.2i:%0.2i:%0.2i.%3i" % [t.hour, t.min, t.sec, t.nsec]
end
if line =~ /fps=\s*(\d*)/
cfps = $1.to_i
else
cfps = 0
end
if line =~ /bitrate=\s*(\d*\.\d*kbits)/
br = $1
else
br = "???"
end
ostr = " %3.2f%% ( %s ) #frame:%i fps:%i bitrate:%s" %
[((csecs/dur_secs)*100), csecs_str, cframe, cfps, br]
print ostr + ("\b" * (ostr.length + 4))
end
end
print "\n"
end

Resources