Skip to content

Commit

Permalink
Feature/save last run (#9)
Browse files Browse the repository at this point in the history
* chore: refactor naming

* feat: skip the first video if it was played the last load

* fix: better method of getting local folder and files
  • Loading branch information
Jared Kotoff authored May 17, 2021
1 parent 7954fca commit 6081507
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ obs-random-video*
*.7z
*.exe
*-linux
*-macos
*-macos
.DS_Store
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/UpDownLeftDie/obs-random-videos/v2

go 1.16

require github.com/sparkdemcisin81/promptui v1.0.0
require (
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/sparkdemcisin81/promptui v1.0.0
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
)
14 changes: 10 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/lunixbochs/vtclean v1.0.0 h1:xu2sLAri4lGiovBDQKxl5mrXyESr3gUr5m5SM5+LVb8=
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/sparkdemcisin81/promptui v1.0.0 h1:GGK4vlIZP36dyM1PgKH0utVkk13kDUqOS5sy6XqeU6I=
github.com/sparkdemcisin81/promptui v1.0.0/go.mod h1:RpZAFCsCFF7OVe+B1Pc4gEA1OAW0n7uQq43ck3rPpNk=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26 changes: 15 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"bufio"
_ "embed"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
Expand All @@ -20,7 +21,7 @@ import (
var templateHtml string

type UserAnswers struct {
VideoFolder string
MediaFolder string
MediaFiles []string
PlayOnlyOne bool
LoopFirstVideo bool
Expand All @@ -30,36 +31,39 @@ type UserAnswers struct {

var outputHtmlName = "obs-random-videos.html"
var audioFileExts = []string{".mp3", ".ogg", ".aac"}
var videoFileExts = []string{".mp4", ".webm", ".mpeg4", ".m4v"}
var videoFileExts = []string{".mp4", ".webm", ".mpeg4", ".m4v", ".mov"}
var mediaFileExts = append(audioFileExts, videoFileExts...)
var promptDelay = 100 * time.Millisecond // helps with race conditionsin promptui

func main() {
currentDir, err := os.Getwd()
currentDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatalf("Failed to get current directory path: %v", err)
}
separator := string(os.PathSeparator)
currentDir += separator
currentDirHTML := currentDir + separator
if runtime.GOOS == "windows" {
separatorEscaped := strings.Repeat(separator, 2)
currentDir = strings.Replace(currentDir, separator, separatorEscaped, -1)
currentDirHTML = strings.Replace(currentDirHTML, separator, separatorEscaped, -1)
}
files, err := ioutil.ReadDir("./")
files, err := os.ReadDir(currentDir)
if err != nil {
log.Fatalf("Failed to read current directory: %v", err)
}
mediaFiles := filterFiles(files, mediaFileExts)
if len(mediaFiles) < 1 {
log.Fatal("No media files found!")
fmt.Printf("No media files found in: %s", currentDir)
fmt.Print("\n\nPress enter to exit...")
input := bufio.NewScanner(os.Stdin)
input.Scan()
return
}
outputHtml, err := os.Create(outputHtmlName)
if err != nil {
log.Fatalf("Failed create output file: %v", err)
}

answers, err := askQuestions(currentDir, mediaFiles)
answers, err := askQuestions(currentDirHTML, mediaFiles)
if err != nil {
outputHtml.Close()
os.Remove(outputHtmlName)
Expand All @@ -82,7 +86,7 @@ func main() {

func askQuestions(currentDir string, mediaFiles []string) (UserAnswers, error) {
answers := UserAnswers{
VideoFolder: currentDir,
MediaFolder: currentDir,
MediaFiles: mediaFiles,
PlayOnlyOne: false,
LoopFirstVideo: false,
Expand Down Expand Up @@ -117,7 +121,7 @@ func removeTransitionVideo(transitionVideo string, mediaFiles []string) []string
return files
}

func filterFiles(files []fs.FileInfo, fileExts []string) []string {
func filterFiles(files []fs.DirEntry, fileExts []string) []string {
filteredFiles := []string{}
for _, f := range files {
for _, ext := range fileExts {
Expand Down
39 changes: 24 additions & 15 deletions template.gohtml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<html>
<head>
<script>
const videoFolder = "{{ .VideoFolder }}";
const mediaFiles = ["{{ StringsJoin .MediaFiles "\", \"" }}"];
const mediaFolder = "{{ .MediaFolder }}";
const initMediaFiles = ["{{ StringsJoin .MediaFiles "\", \"" }}"];
const transitionVideo = "{{ .TransitionVideo }}";
const playOnlyOne = {{ .PlayOnlyOne }};
const loopFirstVideo = {{ .LoopFirstVideo }};

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

Expand All @@ -23,41 +23,46 @@
}

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

if (playOnlyOne) {
if (count > 1) {
// Remove video after playing once
player
.getElementsByClassName('mp4Source')[0]
.removeAttribute('src');
mp4Source.removeAttribute('src');
currentMp4Source.removeAttribute('src');
nextMp4Source.removeAttribute('src');
player.load();
nextPlayer.load();
}
} else {
// TODO: we can use this opacity to crossfade between videos
// 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 = `${videoFolder}${videos[count]}`;
let video = `${mediaFolder}${mediaFiles[count]}`;

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

const currentVideo = currentMp4Source.getAttribute("src");
if (currentVideo !== transitionVideoPath) {
localStorage.setItem("lastPlayed", currentVideo);
}
player.play();
}
}
Expand Down Expand Up @@ -86,9 +91,13 @@
const player2 = document.getElementById("videoPlayer2");
player2.addEventListener("ended", () => playNext(player, player2), {passive: true});

{{/* First run logic */}}
/* First run logic */
const mp4Source = player.getElementsByClassName("mp4Source")[0];
const video = `${videoFolder}${videos[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();

Expand Down

0 comments on commit 6081507

Please sign in to comment.