From 735b5573a8af2edb7b1fa99fa99e3f1649039f2c Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Fri, 8 Mar 2019 10:48:04 +0100 Subject: [PATCH] change xml playlist format to json --- resources/js/custom.js | 105 ++++++++++--------------- resources/list_op.php | 169 +++++++++++++++++++++++++---------------- resources/sh/fill.sh | 18 ++--- 3 files changed, 151 insertions(+), 141 deletions(-) diff --git a/resources/js/custom.js b/resources/js/custom.js index 55f29c60..7c5da4ff 100644 --- a/resources/js/custom.js +++ b/resources/js/custom.js @@ -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 = ''; } 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!
Please fix that...", 'auto', 'auto'); + modal(true, null, null, "Seek in Video", "Seek Value is bigger then Duration!
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!
Please fix that...", 'auto', 'auto'); + modal(true, null, null, "Cut Video", "Cut Value is bigger then Duration!
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", '
', 'auto', 'auto'); + modal(false, null, null, "Start Playout", '
', 'auto', 'auto', function(result) {}); }, success: function(result) { - modal(true, null, null, "Start Playout", '
' + result + '
', 'auto', 'auto'); + modal(true, null, null, "Start Playout", '
' + result + '
', '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", '
Are you really sure, you want to do this?
', 'auto', 'auto', function(result) { + if (result) { + $.ajax({ + url: "resources/player.php", + type: "POST", + data: "playout=stop", + beforeSend: function() { + modal(false, null, null, "Stop Playout", '
', 'auto', 'auto', function(result) {}); + }, + success: function(result) { + modal(true, null, null, "Stop Playout", '
' + result + '
', 'auto', 'auto', function(result) {}); - click: function() { - $.ajax({ - url: "resources/player.php", - type: "POST", - data: "playout=stop", - beforeSend: function() { - modal(false, null, null, "Stop Playout", '
', 'auto', 'auto'); - }, - success: function(result) { - modal(true, null, null, "Stop Playout", '
' + result + '
', '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!
Difference: " + over_length, 'auto', 'auto'); + modal(true, null, null, "Save Playlist", "Playtime from Playlist is to long!
Difference: " + over_length, 'auto', 'auto', function(result) {}); } else if (over_length < -6) { - modal(true, null, null, "Save Playlist", "Playtime from Playlist is to short!
Difference: " + over_length, 'auto', 'auto'); + modal(true, null, null, "Save Playlist", "Playtime from Playlist is to short!
Difference: " + 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!
Difference: " + missed_length, 'auto', 'auto'); + modal(true, null, null, "Fill Playlist", "Playtime from Playlist is to long!
Difference: " + missed_length, 'auto', 'auto', function(result) {}); } else if (missed_length > -6) { - modal(true, null, null, "Fill Playlist", "Playtime from Playlist is in range!
No change will made...", 'auto', 'auto'); + modal(true, null, null, "Fill Playlist", "Playtime from Playlist is in range!
No change will made...", 'auto', 'auto', function(result) {}); } else if (missed_length < -2700) { - modal(true, null, null, "Fill Playlist", "Missed length to fill is bigger then 45 minutes!
Please add more clips...", 'auto', 'auto'); + modal(true, null, null, "Fill Playlist", "Missed length to fill is bigger then 45 minutes!
Please add more clips...", '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 + "
Filled Time: " + moment.utc(Math.abs(missed_length) * 1000).format('HH:mm:ss'), 'auto', 'auto'); - get_xml(date, false); + modal(true, null, null, "Fill Playlist", result + "
Filled Time: " + moment.utc(Math.abs(missed_length) * 1000).format('HH:mm:ss'), 'auto', 'auto', function(result) {}); + get_json(date, false); } }); } diff --git a/resources/list_op.php b/resources/list_op.php index 649abb83..d0d69bb3 100644 --- a/resources/list_op.php +++ b/resources/list_op.php @@ -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 = ''; @@ -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(' - - - - - - - %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('&', '&', $rawline->src); - $xml_str .= sprintf(' \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(' - - - - - - - %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('&', '&', $rawline->src); - $xml_str .= sprintf(' \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); } ?> diff --git a/resources/sh/fill.sh b/resources/sh/fill.sh index 87b3e798..27d4402e 100644 --- a/resources/sh/fill.sh +++ b/resources/sh/fill.sh @@ -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/&/&/g' ) - list+=$( printf '