Skip to content

Commit

Permalink
fix: output file on mac, break out js form html for testing in the fu…
Browse files Browse the repository at this point in the history
…ture
  • Loading branch information
jaredkotoff committed May 17, 2021
1 parent 6081507 commit d1f6930
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 92 deletions.
17 changes: 17 additions & 0 deletions js/body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const player = document.getElementById("videoPlayer1");
const player2 = document.getElementById("videoPlayer2");
player.addEventListener("ended", () => playNext(player2, player), {passive: true});
player2.addEventListener("ended", () => playNext(player, player2), {passive: true});

/* Initial logic */

const mp4Source = player.getElementsByClassName("mp4Source")[0];
let video = `${mediaFolder}${mediaFiles[0]}`;
// check if we played this video last run
if (mediaFiles.length > 1 && localStorage.getItem("lastPlayed") === video) {
video = `${mediaFolder}${mediaFiles[1]}`;
}
mp4Source.setAttribute("src", video);
player.load();

playNext(player, player2);
65 changes: 65 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const mediaFolder = "{{ .MediaFolder }}";
const initMediaFiles = ["{{ StringsJoin .MediaFiles "\", \"" }}"];
const transitionVideo = "{{ .TransitionVideo }}";
const playOnlyOne = {{ .PlayOnlyOne }};
const loopFirstVideo = {{ .LoopFirstVideo }};
const mediaFiles = shuffleArr(initMediaFiles);
let count = 0;
let isTransition = true;
function shuffleArr(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function playNext(player, nextPlayer) {
const nextMp4Source = nextPlayer.getElementsByClassName("mp4Source")[0];
const currentMp4Source = player.getElementsByClassName('mp4Source')[0];
if(!loopFirstVideo) {
if (!transitionVideo || !isTransition) {
count++;
if (count > mediaFiles.length - 1) count = 0;
}
}
if (playOnlyOne) {
if (count > 1) {
// Remove video after playing once
currentMp4Source.removeAttribute('src');
nextMp4Source.removeAttribute('src');
player.load();
nextPlayer.load();
}
} else {
// TODO: we can use this opacity to crossfade between mediaFiles
player.style["z-index"] = 1;
player.style["opacity"] = 1;
nextPlayer.style["z-index"] = 0;
nextPlayer.style["opacity"] = 0;
let video = `${mediaFolder}${mediaFiles[count]}`;
const transitionVideoPath = `${mediaFolder}${transitionVideo}`
if (transitionVideo && transitionVideo !== "" && isTransition) {
video = transitionVideoPath;
isTransition = false;
} else {
isTransition = true;
}
nextMp4Source.setAttribute("src", video);
nextPlayer.load();
nextPlayer.pause();
const currentVideo = currentMp4Source.getAttribute("src");
if (currentVideo !== transitionVideoPath) {
localStorage.setItem("lastPlayed", currentVideo);
}
player.play();
}
}
45 changes: 34 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"bytes"
_ "embed"
"errors"
"fmt"
Expand All @@ -20,6 +21,12 @@ import (
//go:embed template.gohtml
var templateHtml string

//go:embed js/main.js
var mainScript string

//go:embed js/body.js
var bodyScript string

type UserAnswers struct {
MediaFolder string
MediaFiles []string
Expand All @@ -29,6 +36,16 @@ type UserAnswers struct {
TransitionVideo string
}

type Scripts struct {
MainScript string
BodyScript string
}

var scripts = Scripts{
MainScript: mainScript,
BodyScript: bodyScript,
}

var outputHtmlName = "obs-random-videos.html"
var audioFileExts = []string{".mp3", ".ogg", ".aac"}
var videoFileExts = []string{".mp4", ".webm", ".mpeg4", ".m4v", ".mov"}
Expand All @@ -42,6 +59,7 @@ func main() {
}
separator := string(os.PathSeparator)
currentDirHTML := currentDir + separator
outputHtmlName = currentDirHTML + outputHtmlName
if runtime.GOOS == "windows" {
separatorEscaped := strings.Repeat(separator, 2)
currentDirHTML = strings.Replace(currentDirHTML, separator, separatorEscaped, -1)
Expand All @@ -58,30 +76,35 @@ func main() {
input.Scan()
return
}
outputHtml, err := os.Create(outputHtmlName)
if err != nil {
log.Fatalf("Failed create output file: %v", err)
}

answers, err := askQuestions(currentDirHTML, mediaFiles)
if err != nil {
outputHtml.Close()
os.Remove(outputHtmlName)
log.Fatalf("Something went wrong getting user input: %v", err)
}
if answers.TransitionVideo != "" {
answers.MediaFiles = removeTransitionVideo(answers.TransitionVideo, answers.MediaFiles)
}

templateHtml = "<!--\nAUTO GENERATED FILE\nDON'T TOUCH\n-->\n" + templateHtml
t := template.Must(template.New("html").Funcs(template.FuncMap{"StringsJoin": strings.Join}).Parse(templateHtml))
err = t.Execute(outputHtml, answers)
var outputHtml bytes.Buffer
t := template.Must(template.New("html").Parse(templateHtml))
err = t.Execute(&outputHtml, scripts)
if err != nil {
outputHtml.Close()
os.Remove(outputHtmlName)
log.Fatalf("Failed compiling template: %v", err)
}
outputHtml.Close()
t = template.Must(template.New("html").Funcs(template.FuncMap{"StringsJoin": strings.Join}).Parse(outputHtml.String()))
outputHtml.Reset()
err = t.Execute(&outputHtml, answers)
if err != nil {
log.Fatalf("Failed compiling template final: %v", err)
}
outputHtmlFile, err := os.Create(outputHtmlName)
if err != nil {
log.Fatalf("Failed create output file: %v", err)
}
outputHtmlFile.WriteString(outputHtml.String())
outputHtmlFile.Close()
os.Exit(0)
}

func askQuestions(currentDir string, mediaFiles []string) (UserAnswers, error) {
Expand Down
83 changes: 2 additions & 81 deletions template.gohtml
Original file line number Diff line number Diff line change
@@ -1,71 +1,7 @@
<html>
<head>
<script>
const mediaFolder = "{{ .MediaFolder }}";
const initMediaFiles = ["{{ StringsJoin .MediaFiles "\", \"" }}"];
const transitionVideo = "{{ .TransitionVideo }}";
const playOnlyOne = {{ .PlayOnlyOne }};
const loopFirstVideo = {{ .LoopFirstVideo }};

const mediaFiles = shuffleArr(initMediaFiles);
let count = 0;
let isTransition = true;

function shuffleArr(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}

function playNext(player, nextPlayer) {
const nextMp4Source = nextPlayer.getElementsByClassName("mp4Source")[0];
const currentMp4Source = player.getElementsByClassName('mp4Source')[0];
if(!loopFirstVideo) {
if (!transitionVideo || !isTransition) {
count++;
if (count > mediaFiles.length - 1) count = 0;
}
}

if (playOnlyOne) {
if (count > 1) {
// Remove video after playing once
currentMp4Source.removeAttribute('src');
nextMp4Source.removeAttribute('src');
player.load();
nextPlayer.load();
}
} else {
// TODO: we can use this opacity to crossfade between mediaFiles
player.style["z-index"] = 1;
player.style["opacity"] = 1;
nextPlayer.style["z-index"] = 0;
nextPlayer.style["opacity"] = 0;
let video = `${mediaFolder}${mediaFiles[count]}`;

const transitionVideoPath = `${mediaFolder}${transitionVideo}`
if (transitionVideo && transitionVideo !== "" && isTransition) {
video = transitionVideoPath;
isTransition = false;
} else {
isTransition = true;
}
nextMp4Source.setAttribute("src", video);
nextPlayer.load();
nextPlayer.pause();

const currentVideo = currentMp4Source.getAttribute("src");
if (currentVideo !== transitionVideoPath) {
localStorage.setItem("lastPlayed", currentVideo);
}
player.play();
}
}
{{ .MainScript }}
</script>
<style>
html,
Expand All @@ -86,22 +22,7 @@
<source src="" class="mp4Source" type="video/mp4" />
</video>
<script>
const player = document.getElementById("videoPlayer1");
player.addEventListener("ended", () => playNext(player2, player), {passive: true});
const player2 = document.getElementById("videoPlayer2");
player2.addEventListener("ended", () => playNext(player, player2), {passive: true});

/* First run logic */
const mp4Source = player.getElementsByClassName("mp4Source")[0];
let video = `${mediaFolder}${mediaFiles[0]}`;
// check if we played this video last run
if (mediaFiles.length > 1 && localStorage.getItem("lastPlayed") === video) {
video = `${mediaFolder}${mediaFiles[1]}`;
}
mp4Source.setAttribute("src", video);
player.load();

playNext(player, player2);
{{ .BodyScript }}
</script>
</body>
</html>

0 comments on commit d1f6930

Please sign in to comment.