From d1f69300ef880f5df104e996d17b1c56634ccd35 Mon Sep 17 00:00:00 2001 From: jaredkotoff Date: Mon, 17 May 2021 14:44:01 -0700 Subject: [PATCH] fix: output file on mac, break out js form html for testing in the future --- js/body.js | 17 ++++++++++ js/main.js | 65 ++++++++++++++++++++++++++++++++++++++ main.go | 45 ++++++++++++++++++++------- template.gohtml | 83 ++----------------------------------------------- 4 files changed, 118 insertions(+), 92 deletions(-) create mode 100644 js/body.js create mode 100644 js/main.js diff --git a/js/body.js b/js/body.js new file mode 100644 index 0000000..3b97ca4 --- /dev/null +++ b/js/body.js @@ -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); \ No newline at end of file diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..17182ec --- /dev/null +++ b/js/main.js @@ -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(); + } +} \ No newline at end of file diff --git a/main.go b/main.go index 5a00ee7..62f0628 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "bytes" _ "embed" "errors" "fmt" @@ -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 @@ -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"} @@ -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) @@ -58,15 +76,9 @@ 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 != "" { @@ -74,14 +86,25 @@ func main() { } templateHtml = "\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) { diff --git a/template.gohtml b/template.gohtml index 69620d8..c748bbe 100644 --- a/template.gohtml +++ b/template.gohtml @@ -1,71 +1,7 @@