change xml playlist format to json

This commit is contained in:
jb-alvarado 2019-03-08 10:48:04 +01:00
parent f7489246d4
commit 735b5573a8
3 changed files with 151 additions and 141 deletions

View File

@ -4,7 +4,7 @@ global functions
// modal function:
// display an overlay window for warings, clip previews, etc.
function modal(btOK, btCan, video, title, content, x, y) {
function modal(btOK, btCan, video, title, content, x, y, callback) {
if (video) {
text = '<video id="preview_player" class="video-js" controls preload="auto" autoplay="true" data-setup={}> <source src="' + content + '" type="video/mp4" /> </video>';
} else {
@ -28,6 +28,7 @@ function modal(btOK, btCan, video, title, content, x, y) {
click: function() {
$('#preview_player').remove()
$(this).dialog("close");
callback(true);
}
},
{
@ -36,6 +37,7 @@ function modal(btOK, btCan, video, title, content, x, y) {
click: function() {
$(this).dialog("close");
callback(false);
}
}
]
@ -75,10 +77,10 @@ $('.calender').pignoseCalendar({
if (date[0].format('YYYY-MM-DD') === list_date.format('YYYY-MM-DD')) {
$('#playlistBody').attr('listday', list_date.format('YYYY-MM-DD'));
get_xml(list_date.format('YYYY-MM-DD'), true);
get_json(list_date.format('YYYY-MM-DD'), true);
} else {
$('#playlistBody').attr('listday', date[0].format('YYYY-MM-DD'));
get_xml(date[0].format('YYYY-MM-DD'), true);
get_json(date[0].format('YYYY-MM-DD'), true);
}
});
}
@ -128,7 +130,7 @@ function init_browse_click() {
var current_element = $(this);
var rawpath = current_element.closest('li').attr('data-href').replace(/^\?dir=/g, current_element);
modal(true, null, true, decodeURIComponent(rawpath.split("/").pop()), rawpath, 1039, 716);
modal(true, null, true, decodeURIComponent(rawpath.split("/").pop()), rawpath, 1039, 716, function(result) {});
e.preventDefault();
});
@ -157,7 +159,7 @@ window.onload = function() {
// write playlist date to list attribute, for later use
$('#playlistBody').attr('listday', list_date.format("YYYY-MM-DD"));
// read playlist from current day
get_xml(list_date.format("YYYY-MM-DD"), true);
get_json(list_date.format("YYYY-MM-DD"), true);
});
}
@ -173,7 +175,7 @@ function init_list_op() {
var file_path = $(this).attr('data-href');
var play_URL = encodeURIComponent(file_path);
modal(true, null, true, decodeURIComponent(file_path.split("/").pop()), play_URL, 1039, 716);
modal(true, null, true, decodeURIComponent(file_path.split("/").pop()), play_URL, 1039, 716, function(result) {});
e.preventDefault();
});
@ -192,7 +194,7 @@ function init_list_op() {
setTimeout(enableButton, 1000);
return true;
} else {
modal(true, null, null, "Delete Item", "Removing items is limited to every second.", 'auto', 'auto');
modal(true, null, null, "Delete Item", "Removing items is limited to every second.", 'auto', 'auto', function(result) {});
}
});
@ -201,7 +203,7 @@ function init_list_op() {
var in_seconds = moment.duration($(this).val()).asSeconds();
var in_duration = $(this).closest('ul').parent().attr('dur');
if (in_seconds > in_duration) {
modal(true, null, null, "Seek in Video", "Seek Value is bigger then Duration!<br/>Please fix that...", 'auto', 'auto');
modal(true, null, null, "Seek in Video", "Seek Value is bigger then Duration!<br/>Please fix that...", 'auto', 'auto', function(result) {});
$(this).val("00:00:00");
} else {
$(this).closest('ul').parent().attr('in', in_seconds);
@ -215,7 +217,7 @@ function init_list_op() {
var out_seconds = moment.duration($(this).val()).asSeconds();
var out_dur = $(this).closest('ul').parent().attr('dur');
if (out_seconds > out_dur) {
modal(true, null, null, "Cut Video", "Cut Value is bigger then Duration!<br/>Please fix that...", 'auto', 'auto');
modal(true, null, null, "Cut Video", "Cut Value is bigger then Duration!<br/>Please fix that...", 'auto', 'auto', function(result) {});
$(this).val(cur_val);
} else {
$(this).closest('ul').parent().attr('out', out_seconds);
@ -226,13 +228,13 @@ function init_list_op() {
// reset button
$('#bt_reset').click(function() {
// scroll to playlist top
get_xml($('#playlistBody').attr('listday'), false);
get_json($('#playlistBody').attr('listday'), false);
});
}
// read formated playlist from php function
function get_xml(date, jump) {
$.get("resources/list_op.php?xml_path=" + date, function(result) {
function get_json(date, jump) {
$.get("resources/list_op.php?json_path=" + date, function(result) {
$('#playlistBody').html(result);
init_list_op();
if (jump) {
@ -400,10 +402,10 @@ $('#bt_start').click(function() {
type: "POST",
data: "playout=start",
beforeSend: function() {
modal(false, null, null, "Start Playout", '<div style="text-align:center; min-width: 120px"><img src="resources/img/35.png" height="46" width="46"></div>', 'auto', 'auto');
modal(false, null, null, "Start Playout", '<div style="text-align:center; min-width: 120px"><img src="resources/img/35.png" height="46" width="46"></div>', 'auto', 'auto', function(result) {});
},
success: function(result) {
modal(true, null, null, "Start Playout", '<div style="text-align:center;min-width: 120px">' + result + '</div>', 'auto', 'auto');
modal(true, null, null, "Start Playout", '<div style="text-align:center;min-width: 120px">' + result + '</div>', 'auto', 'auto', function(result) {});
videojs('myStream').play();
get_track_list(true);
@ -414,47 +416,24 @@ $('#bt_start').click(function() {
// stop stream
$('#bt_stop').click(function() {
$('#dialog-confirm').html("Are you really sure, you want to do this?");
$("#dialog-confirm").dialog({
title: "Stop Playout",
resizable: false,
height: 'auto',
width: 'auto',
modal: true,
buttons: [{
id: "button-ok",
text: "Ok",
modal(true, true, null, "Stop Playout", '<div style="text-align:center;min-width: 120px">Are you really sure, you want to do this?</div>', 'auto', 'auto', function(result) {
if (result) {
$.ajax({
url: "resources/player.php",
type: "POST",
data: "playout=stop",
beforeSend: function() {
modal(false, null, null, "Stop Playout", '<div style="text-align:center; min-width: 120px"><img src="resources/img/35.png" height="46" width="46"></div>', 'auto', 'auto', function(result) {});
},
success: function(result) {
modal(true, null, null, "Stop Playout", '<div style="text-align:center;min-width: 120px">' + result + '</div>', 'auto', 'auto', function(result) {});
click: function() {
$.ajax({
url: "resources/player.php",
type: "POST",
data: "playout=stop",
beforeSend: function() {
modal(false, null, null, "Stop Playout", '<div style="text-align:center; min-width: 120px"><img src="resources/img/35.png" height="46" width="46"></div>', 'auto', 'auto');
},
success: function(result) {
modal(true, null, null, "Stop Playout", '<div style="text-align:center;min-width: 120px">' + result + '</div>', 'auto', 'auto');
videojs('myStream').pause();
get_track_list(false);
},
});
}
},
{
id: "button-cancel",
text: "Cancel",
click: function() {
$(this).dialog("close");
}
}
]
videojs('myStream').pause();
get_track_list(false);
},
});
}
});
});
/* -----------------------------------------------------------------------------
@ -535,9 +514,9 @@ $(document).ready(function() {
var over_length = last_start - start_time + last_out - last_in - 86400;
if (over_length > 0) {
modal(true, null, null, "Save Playlist", "Playtime from Playlist is to long!<br/><b>Difference:</b> " + over_length, 'auto', 'auto');
modal(true, null, null, "Save Playlist", "Playtime from Playlist is to long!<br/><b>Difference:</b> " + over_length, 'auto', 'auto', function(result) {});
} else if (over_length < -6) {
modal(true, null, null, "Save Playlist", "Playtime from Playlist is to short!<br/><b>Difference:</b> " + over_length, 'auto', 'auto');
modal(true, null, null, "Save Playlist", "Playtime from Playlist is to short!<br/><b>Difference:</b> " + over_length, 'auto', 'auto', function(result) {});
} else {
var save_list = [];
$('#playlistBody li.list-item').each(function(){
@ -558,7 +537,7 @@ $(document).ready(function() {
url: "resources/list_op.php",
data: "date=" + date + "&save=" + json,
success: function(result) {
modal(true, null, null, "Save Playlist", result, 'auto', 'auto');
modal(true, null, null, "Save Playlist", result, 'auto', 'auto', function(result) {});
}
});
}
@ -573,11 +552,11 @@ $(document).ready(function() {
var missed_length = last_start - start_time + last_out - last_in - 86400;
if (missed_length > 0) {
modal(true, null, null, "Fill Playlist", "Playtime from Playlist is to long!<br/><b>Difference:</b> " + missed_length, 'auto', 'auto');
modal(true, null, null, "Fill Playlist", "Playtime from Playlist is to long!<br/><b>Difference:</b> " + missed_length, 'auto', 'auto', function(result) {});
} else if (missed_length > -6) {
modal(true, null, null, "Fill Playlist", "Playtime from Playlist is in range!<br/><b>No change will made...</b>", 'auto', 'auto');
modal(true, null, null, "Fill Playlist", "Playtime from Playlist is in range!<br/><b>No change will made...</b>", 'auto', 'auto', function(result) {});
} else if (missed_length < -2700) {
modal(true, null, null, "Fill Playlist", "Missed length to fill is bigger then 45 minutes!<br/><b>Please add more clips...</b>", 'auto', 'auto');
modal(true, null, null, "Fill Playlist", "Missed length to fill is bigger then 45 minutes!<br/><b>Please add more clips...</b>", 'auto', 'auto', function(result) {});
} else {
date = $('#playlistBody').attr('listday');
var save_list = [];
@ -597,15 +576,15 @@ $(document).ready(function() {
$.ajax({
type: "POST",
url: "resources/list_op.php",
data: "fill_playlist=" + date + "&diff_len=" + Math.abs(missed_length) + "&start_time=" + (last_start + last_out - last_in ) + "&old_list=" + json,
data: "fill_playlist=" + date + "&diff_len=" + Math.abs(missed_length) + "&start_time=" + (last_start + last_out - last_in) + "&old_list=" + json,
beforeSend: function() {
modal(null, null, null, "Fill Playlist", "Filling Playlist in progress...", 'auto', 'auto');
modal(null, null, null, "Fill Playlist", "Filling Playlist in progress...", 'auto', 'auto', function(result) {});
},
success: function(result) {
// console.log(result);
$('#dialog-confirm').dialog("close");
modal(true, null, null, "Fill Playlist", result + "<br/><b>Filled Time:</b> " + moment.utc(Math.abs(missed_length) * 1000).format('HH:mm:ss'), 'auto', 'auto');
get_xml(date, false);
modal(true, null, null, "Fill Playlist", result + "<br/><b>Filled Time:</b> " + moment.utc(Math.abs(missed_length) * 1000).format('HH:mm:ss'), 'auto', 'auto', function(result) {});
get_json(date, false);
}
});
}

View File

@ -44,7 +44,7 @@ if (!empty($_GET['time_shift'])) {
}
/* -----------------------------------------------------------------------------
xml playlist operations
json playlist operations
------------------------------------------------------------------------------*/
// file extension to filter
@ -60,21 +60,28 @@ $except = array(
$ext = implode('|', $except);
// read from xml file
// read from json file
// formate the values and generate readeble output
if(!empty($_GET['xml_path'])) {
$xml_date = $_GET['xml_path'];
$date_str = explode('-', $xml_date);
if(!empty($_GET['json_path'])) {
$json_date = $_GET['json_path'];
$date_str = explode('-', $json_date);
$get_ini = get_config();
$get_dir = "/^playlist_path.*\$/m";
preg_match_all($get_dir, $get_ini, $matches);
$line = implode("\n", $matches[0]);
$path_root = explode("= ", $line)[1];
$xml_path = $path_root . "/" . $date_str[0] . "/" . $date_str[1] . "/" . $xml_date . ".xml";
$json_path = $path_root . "/" . $date_str[0] . "/" . $date_str[1] . "/" . $json_date . ".json";
if (file_exists($xml_path)) {
$xml = simplexml_load_file($xml_path) or die("Error: Cannot create object");
if (file_exists($json_path)) {
$content = file_get_contents($json_path) or die("Error: Cannot create object");
$json = json_decode($content, true);
list($hh, $mm, $ss) = explode(":", $json["begin"]);
list($l_hh, $l_mm, $l_ss) = explode(":", $json["length"]);
$start = $hh * 3600 + $mm * 60 + $ss;
$length = $l_hh * 3600 + $l_mm * 60 + $l_ss;
$src_re = array();
$src_re[0] = '/# [0-9-]+.('.$ext.')$/';
@ -82,19 +89,21 @@ if(!empty($_GET['xml_path'])) {
$src_re[2] = '/.('.$ext.')$/';
$src_re[3] = '/^# /';
foreach($xml->body[0]->video as $video) {
$src = preg_replace('/^\//', '', $video['src']);
$src_arr = explode('/', $src);
$name = preg_replace($src_re, '', end($src_arr));
$name = str_replace('§', '?', $name);
$clipBegin = $video['begin'];
$begin = gmdate("H:i:s", intval($clipBegin));
$dur = $video['dur'];
$duration = gmdate("H:i:s", intval($dur));
$in = $video['in'];
$in_p = gmdate("H:i:s", intval($in));
$out = $video['out'];
$out_p = gmdate("H:i:s", intval($out));
foreach($json["program"] as $video) {
$src = preg_replace('/^\//', '', $video['source']);
$src_arr = explode('/', $src);
$name = preg_replace($src_re, '', end($src_arr));
$name = str_replace('§', '?', $name);
$clipBegin = $start;
$begin = gmdate("H:i:s", intval($clipBegin));
$dur = $video['duration'];
$duration = gmdate("H:i:s", intval($dur));
$in = $video['in'];
$in_p = gmdate("H:i:s", intval($in));
$out = $video['out'];
$out_p = gmdate("H:i:s", intval($out));
$start += $out - $in;
$play = '<a href="#" data-href="' . $src . '" class="file-play"><i class="fa fa-play-circle file-op"></i></a>';
@ -136,7 +145,7 @@ if(!empty($_GET['xml_path'])) {
// generate object from dragged item
if(!empty($_GET['li_path'])) {
$path = urldecode($_GET['li_path']);
$path = rawurldecode($_GET['li_path']);
$src_re = array();
$src_re[0] = '/# [0-9-]+.('.$ext.')$/';
@ -181,34 +190,46 @@ if(!empty($_POST['save'])) {
preg_match_all($get_dir, $get_ini, $matches);
$line = implode("\n", $matches[0]);
$path_root = explode("= ", $line)[1];
$xml_path = $path_root . "/" . $date_str[0] . "/" . $date_str[1];
$xml_output = $xml_path . "/" . $date . ".xml";
$json_path = $path_root . "/" . $date_str[0] . "/" . $date_str[1];
$json_output = $json_path . "/" . $date . ".json";
// create xml head
$xml_str = sprintf('<playlist>
<head>
<meta name="author" content="example"/>
<meta name="title" content="Live Stream"/>
<meta name="copyright" content="(c)%s example.org"/>
<meta name="date" content="%s"/>
</head>
<body>%s', $date_str[0], $date, "\n");
$beginRaw = round($raw_arr[0]->begin);
$start = sprintf('%02d:%02d:%02d', ($beginRaw/3600),($beginRaw/60%60), $beginRaw%60);
// create xml video element
// prepare header
$list = array(
"channel" => "Test 1",
"date" => $date,
"begin" => $start,
"length" => "24:00:00.000",
"program" => []
);
$length = 0;
// create json video element
foreach($raw_arr as $rawline) {
$formated_src = str_replace('&', '&amp;', $rawline->src);
$xml_str .= sprintf(' <video src="%s" begin="%s" dur="%s" in="%s" out="%s"/>%s', $formated_src, $rawline->begin, $rawline->dur, $rawline->in, $rawline->out, "\n");
$clipItem = array(
"in" => floatval($rawline->in),
"out" => floatval($rawline->out),
"duration" => floatval($rawline->dur),
"source" => intval($rawline->src)
);
$list["program"][] = $clipItem;
$length += round($rawline->out - $rawline->in);
}
// crate xml end
$xml_str .= " </body>\n</playlist>\n";
$list["program"]["length"] = sprintf('%02d:%02d:%02d', ($length/3600),($length/60%60), $length%60);
if (!is_dir($xml_path)) {
mkdir($xml_path, 0777, true);
if (!is_dir($json_path)) {
mkdir($json_path, 0777, true);
}
file_put_contents($xml_output, $xml_str);
printf('Save playlist "%s.xml" done...', $date);
file_put_contents($json_output, json_encode(
$list, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
printf('Save playlist "%s.json" done...', $date);
}
// fill playlist to 24 hours
@ -224,38 +245,56 @@ if(!empty($_POST['fill_playlist'])) {
preg_match_all($get_dir, $get_ini, $matches);
$line = implode("\n", $matches[0]);
$path_root = explode("= ", $line)[1];
$xml_path = $path_root . "/" . $date_str[0] . "/" . $date_str[1];
$xml_output = $xml_path . "/" . $list_date . ".xml";
$json_path = $path_root . "/" . $date_str[0] . "/" . $date_str[1];
$json_output = $json_path . "/" . $list_date . ".json";
$fill = shell_exec("./sh/fill.sh '".$list_date."' '".$diff_len."' '".$start_time."'");
$fill = shell_exec("./sh/fill.sh '".$diff_len."'");
// create xml head
$xml_str = sprintf('<playlist>
<head>
<meta name="author" content="example"/>
<meta name="title" content="Live Stream"/>
<meta name="copyright" content="(c)%s example.org"/>
<meta name="date" content="%s"/>
</head>
<body>%s', $date_str[0], $list_date, "\n");
$beginRaw = round($raw_arr[0]->begin);
$start = sprintf('%02d:%02d:%02d', ($beginRaw/3600),($beginRaw/60%60), $beginRaw%60);
// create xml video element
// prepare header
$list = array(
"channel" => "Test 1",
"date" => $list_date,
"begin" => $start,
"length" => "24:00:00.000",
"program" => []
);
// create json video element
foreach($raw_arr as $rawline) {
$formated_src = str_replace('&', '&amp;', $rawline->src);
$xml_str .= sprintf(' <video src="%s" begin="%s" dur="%s" in="%s" out="%s"/>%s', $formated_src, $rawline->begin, $rawline->dur, $rawline->in, $rawline->out, "\n");
$clipItem = array(
"in" => floatval($rawline->in),
"out" => floatval($rawline->out),
"duration" => floatval($rawline->dur),
"source" => $rawline->src
);
$list["program"][] = $clipItem;
}
// add filled clips
$xml_str .= $fill;
foreach(preg_split("/((\r?\n)|(\r\n?))/", $fill) as $line){
$line_arr = explode('|', $line);
// crate xml end
$xml_str .= " </body>\n</playlist>\n";
$clipItem = array(
"in" => floatval($line_arr[0]),
"out" => floatval($line_arr[1]),
"duration" => floatval($line_arr[2]),
"source" => $line_arr[3]
);
if (!is_dir($xml_path)) {
mkdir($xml_path, 0777, true);
if ($line_arr[3]) {
$list["program"][] = $clipItem;
}
}
file_put_contents($xml_output, $xml_str);
printf('Filled and save playlist "%s.xml" done...', $list_date);
if (!is_dir($json_path)) {
mkdir($json_path, 0777, true);
}
file_put_contents($json_output, json_encode(
$list, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
printf('Filled and save playlist "%s.json" done...', $list_date);
}
?>

View File

@ -5,25 +5,19 @@ filler_path=$( awk -F' = ' '/^filler_path/{ print $2 }' /etc/ffplayout/ffplayou
filler=$( awk -F' = ' '/^filler_clip/{ print $2 }' /etc/ffplayout/ffplayout.conf )
filler_dur=$( ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$filler" )
date=$1
diff=$2
start=$3
diff=$1
list=''
while read -r file; do
dur=$( ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" )
if (( $(bc <<< "$diff-$dur>170") )); then
name=$( echo "$file" | sed 's/&/&amp;/g' )
list+=$( printf ' <video src="%s" begin="%s" dur="%s" in="%s" out="%s"/>%s' "$name" "$start" "$dur" "0.0" "$dur" "\n" )
list+="0.0|$dur|$dur|$file\n"
start=$( echo "$start + $dur" | bc )
diff=$( echo "$diff - $dur" | bc )
elif (( $(bc <<< "$diff<=$dur+5 && $diff>=$dur") )); then
name=$( echo "$file" | sed 's/&/&amp;/g' )
list+=$( printf ' <video src="%s" begin="%s" dur="%s" in="%s" out="%s"/>%s' "$name" "$start" "$dur" "0.0" "$dur" "\n" )
list+="0.0|$dur|$dur|$file\n"
start=$( echo "$start + $dur" | bc )
diff=$( echo "$diff - $dur" | bc )
break
fi
@ -33,10 +27,8 @@ if (( $(bc <<< "$diff>10") )); then
while read -r file; do
dur=$( ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" )
if (( $(bc <<< "$diff<=$dur+5 && $diff>=$dur") )); then
name=$( echo "$file" | sed 's/&/&amp;/g' )
list+=$( printf ' <video src="%s" begin="%s" dur="%s" in="%s" out="%s"/>%s' "$name" "$start" "$dur" "0.0" "$dur" "\n" )
list+="0.0|$dur|$dur|$file\n"
start=$( echo "$start + $dur" | bc )
diff=$( echo "$diff - $dur" | bc )
break
fi
@ -44,6 +36,6 @@ if (( $(bc <<< "$diff>10") )); then
fi
seek=$( echo "$filler_dur - $diff" | bc )
list+=$( printf ' <video src="%s" begin="%s" dur="%s" in="%s" out="%s"/>%s' "$filler" "$start" "$filler_dur" "$seek" "$filler_dur" "\n" )
list+="$seek|$filler_dur|$filler_dur|$filler\n"
printf "$list"