Skip to content

Instantly share code, notes, and snippets.

@Lokno
Last active April 29, 2018 19:17
Show Gist options
  • Select an option

  • Save Lokno/2bc1d8117cc0624b10e575ca3aec1828 to your computer and use it in GitHub Desktop.

Select an option

Save Lokno/2bc1d8117cc0624b10e575ca3aec1828 to your computer and use it in GitHub Desktop.
Basic javascript web page that streams a list of videos from a text file.
<!-- Streams a list of videos from a text file -->
<!-- Text file format: integer,url -->
<!-- Tested with Google Chrome Version 66.0.3359.139 (Official Build) (64-bit) -->
<!-- To run a simple local web server -->
<!-- type "python -m http.server 8000" at the command line -->
<!-- then direct your browser to localhost:8000 -->
<html>
<head>
<title>Video Stream</title>
<script type='text/javascript'>
var played = []
var urls = []
var vid_idx = 0
var vid_obj = null
var video_text_file = 'videos.txt'
// gets the last (1) lines form the file (used in getLatest)
var re = /([^\n]+\n){1}$/
// adds new video urls to the queue
function addNewEntries( latest ) {
for (let i in latest) {
var items = latest[i].split(',')
if (items.length == 2 && !urls.includes(items[1]) && !played.includes(items[1])) {
urls.push(items[1])
}
}
}
// retrieves all the video urls from a text file
function getAll(file) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
addNewEntries(rawFile.responseText.trim().split('\n'))
}
}
}
rawFile.send(null);
}
// retrieves the last few lines so the passed file
// and adds them to the url queue
// see line 18 to change the number of lines added
function getLatest(file) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
matches = re.exec(rawFile.responseText)
if (matches != null) {
latest_entries = matches[0].trim().split('\n')
addNewEntries(latest_entries)
}
}
}
}
rawFile.send(null);
}
// called on an interval (Option A)
function update() {
getLatest(video_text_file)
}
// called when the page loads
function init() {
vid_obj = document.getElementById('video')
vid_obj.addEventListener('ended',videoEnded,false)
// Option A: set an interval to add new urls to the queue every (5) seconds
// use this option if the text file is constantly updating
//setInterval(update,5000)
// Option B: read in the entire list form file and play all video urls
getAll(video_text_file)
}
// starts a new video when the previously playing video ends
function videoEnded(e) {
if( urls.length > 0) {
var newVid = urls.pop()
played.push(newVid)
vid_obj.setAttribute('src', newVid)
}
}
</script>
<style>
#video {
position: fixed;
right: 0;
bottom: 0;
min-width: 100%;
min-height: 100%;
background: rgb(0, 0, 0);
}
</style>
</head>
<body onload="init()">
<video autoplay id="video">
<!-- Replace this video with whatever you like to start off with. Needs to be longer than the setInterval time (line 84). -->
<source src="https://video.twimg.com/ext_tw_video/990279051477532672/pu/vid/1280x720/la-RHnV_gziTsA8W.mp4?tag=3" type="video/mp4">
Your browser does not support the HTML5 video tag
</video>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment