Skip to content

Commit

Permalink
generate thumbs in parallel and embed html
Browse files Browse the repository at this point in the history
  • Loading branch information
gadelkareem committed Mar 11, 2022
1 parent 5365572 commit f65e58d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 39 deletions.
3 changes: 1 addition & 2 deletions gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
</header>
<script>
files.forEach(function (p) {
p.replace(/%2F/g, '%2_F');
p = encodeURIComponent(p).replace(/%2F/g, "/").replace(/%2_F/g, '%2F')
p = encodeURIComponent(p.replace(/%2F/g, '%2_F')).replace(/%2F/g, "/").replace(/%2_F/g, '%2F')
const filename = p.split('/').pop()
const video = $('<div><a href="' + p + '" target="_blank"><img alt="' + filename + '" src="thumbs/' + filename + '.png" /></a></div>')

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/gadelkareem/video-gallery-generator

go 1.17

require github.com/gadelkareem/go-helpers v0.0.0-20200725110331-77a434fc0423
require github.com/gadelkareem/go-helpers v0.0.0-20220309204952-b41d4c3bef9a
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/gadelkareem/go-helpers v0.0.0-20200725110331-77a434fc0423 h1:eERtwaJW5EGq/2DH0WzoxQXPaz7kD4N5z8p/qJ2rHfU=
github.com/gadelkareem/go-helpers v0.0.0-20200725110331-77a434fc0423/go.mod h1:NQj6Txfr/VxtlQDQoy14/d6/MyZ5nZX0EatXIVklKpo=
github.com/gadelkareem/go-helpers v0.0.0-20220309204952-b41d4c3bef9a h1:Nb65fWN0H/cnS//M78h015AKRRizznnnla6dsEvWrcQ=
github.com/gadelkareem/go-helpers v0.0.0-20220309204952-b41d4c3bef9a/go.mod h1:NQj6Txfr/VxtlQDQoy14/d6/MyZ5nZX0EatXIVklKpo=
78 changes: 44 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
_ "embed"
"encoding/json"
"flag"
"fmt"
Expand All @@ -17,6 +18,9 @@ import (
"strings"
)

//go:embed gallery.html
var html string

func main() {
port := flag.String("p", "8282", "port to serve on")
d := flag.String("d", ".", "static file folder")
Expand Down Expand Up @@ -56,39 +60,53 @@ func listFiles(d string) []string {

func generateThumbs(d string, fs []string) {
thumbsDir := path.Join(d, "thumbs")
os.MkdirAll(thumbsDir, os.ModePerm)
err := os.MkdirAll(thumbsDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}

wg := h.NewWgExec(3)
for _, f := range fs {
log.Printf("Generating thumbnail for %s\n", f)
thumb := path.Join(thumbsDir, path.Base(f)+".png")
if h.FileExists(thumb) {
log.Printf("Thumbnail already exists for %s\n", f)
continue
}
cmd := fmt.Sprintf("ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 '%s'", f)
//log.Printf("%s\n", cmd)
b, err := exec.Command("/bin/bash", "-c", cmd).Output()
if err != nil {
log.Printf("Error getting video duration for %s, Error: %v\n", f, err)
}
s := strings.TrimSuffix(string(b), "\n")
//log.Printf("Video has %s seconds\n", s)
numSec, _ := strconv.ParseFloat(s, 64)
log.Printf("Video has %f seconds\n", numSec)
cmd = fmt.Sprintf("ffmpeg -y -i '%s' -vf scale=220:-1 -vframes 1 -ss %f '%s'", f, numSec/2, thumb)
//log.Printf("%s\n", cmd)
err = exec.Command("/bin/bash", "-c", cmd).Run()
if err != nil {
log.Printf("Error generating thumbnail for %s, Error: %v\n", f, err)
}
wg.Run(createThumb, thumbsDir, f)
}
wg.Wait()
}

func createThumb(ps ...interface{}) {
thumbsDir := ps[0].(string)
f := ps[1].(string)
log.Printf("Generating thumbnail for %s\n", f)
thumb := path.Join(thumbsDir, path.Base(f)+".png")
if h.FileExists(thumb) {
log.Printf("Thumbnail already exists for %s\n", f)
return
}
cmd := fmt.Sprintf("ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 '%s'", f)
//log.Printf("%s\n", cmd)
b, err := exec.Command("/bin/bash", "-c", cmd).Output()
if err != nil {
log.Printf("Error getting video duration for %s, Error: %v\n", f, err)
}
s := strings.TrimSuffix(string(b), "\n")
//log.Printf("Video has %s seconds\n", s)
numSec, _ := strconv.ParseFloat(s, 64)
log.Printf("%f seconds in '%s'\n", numSec, f)
cmd = fmt.Sprintf("ffmpeg -y -i '%s' -vf scale=220:-1 -vframes 1 -ss %f '%s'", f, numSec/2, thumb)
//log.Printf("%s\n", cmd)
err = exec.Command("/bin/bash", "-c", cmd).Run()
if err != nil {
log.Printf("Error generating thumbnail for %s, Error: %v\n", f, err)
}
}

func writeVars(d string, fs []string) {
log.Printf("Generating Video List")
var fss []string
d = strings.TrimSuffix(d, "/") + "/"
for i, _ := range fs {
fs[i] = strings.Replace(fs[i], d+"/", "", 1)
fss = append(fss, strings.Replace(fs[i], d, "", 1))
}
lj, err := json.Marshal(fs)
lj, err := json.Marshal(fss)
if err != nil {
log.Fatal(err)
}
Expand All @@ -97,16 +115,8 @@ func writeVars(d string, fs []string) {
if err != nil {
log.Fatalf("Error writing list to json file %+v\n", err)
}
pwd, err := os.Getwd()
if err != nil {
log.Fatalf("Error getting current path %+v\n", err)
}

c, err := h.ReadFile(path.Join(pwd, "gallery.html"))
if err != nil {
log.Fatalf("Error reading gallery.html file %+v\n", err)
}
err = h.WriteFile(path.Join(d, "gallery.html"), c)
err = h.WriteFile(path.Join(d, "gallery.html"), html)
if err != nil {
log.Fatalf("Error writing gallery.html file %+v\n", err)
}
Expand Down

0 comments on commit f65e58d

Please sign in to comment.