Skip to content
Permalink
Browse files

Add time between frames as an option to select the number of frames

  • Loading branch information...
HenrySlawniak committed Oct 17, 2018
1 parent ed57a57 commit d4e45c4fd6ec4d9a1ee073299e2252877a226f28
Showing with 34 additions and 18 deletions.
  1. +4 −4 contactsheet.go
  2. +17 −2 main.go
  3. +3 −4 thumbs.go
  4. +10 −8 video.go
@@ -60,11 +60,11 @@ func init() {
}
}

func generateContactSheet(vid *Video, numFrames int) {
func generateContactSheet(vid *Video) {
var FrameWidth int
var FrameHeight int
frames := map[int]image.Image{}
for i := 0; i < numFrames; i++ {
for i := 0; i < vid.ThumbCount; i++ {
frameLoc := filepath.Join(os.TempDir(), fmt.Sprintf("%s-%d.png", vid.SHA1.Hex(), i))
if !FileExists(frameLoc) {
log.Errorf("Frame missing from disk `%s`\n", frameLoc)
@@ -94,7 +94,7 @@ func generateContactSheet(vid *Video, numFrames int) {

log.Infof("Loaded %d frames for %s", len(frames), vid.Filename)

rowCount := int(math.Max(math.Ceil(float64(numFrames / *framesPerRow)), 1))
rowCount := int(math.Max(math.Ceil(float64(vid.ThumbCount / *framesPerRow)), 1))

sheetWidth := (*framesPerRow * FrameWidth) + ((*framesPerRow + 1) * GutterSize)
if sheetWidth < MinSheetWidth {
@@ -169,7 +169,7 @@ func generateContactSheet(vid *Video, numFrames int) {
rect := image.Rect(xOff, yOff, xOff+FrameWidth, yOff+FrameHeight)
draw.Draw(sheet, rect, frame, frame.Bounds().Min, draw.Src)

frameTime := stampToString(((float64(vid.Duration)) / float64(numFrames)) * float64(i))
frameTime := stampToString(((float64(vid.Duration)) / float64(vid.ThumbCount)) * float64(i))
stampSize := FontSize * 0.7
c.SetFontSize(stampSize)
c.SetSrc(textCol)
19 main.go
@@ -32,6 +32,7 @@ import (
"path/filepath"
"runtime"
"strings"
"time"
)

var (
@@ -44,6 +45,7 @@ var (
framesPerRow = flag.Int("frames-per-row", 3, "The number of frames per each row in the final contact sheet")
writeAttribution = flag.Bool("write-attribution", true, "Writed \"Generated by thumbnailer.net\" to contact sheet")
walkDirectories = flag.Bool("walk-directories", true, "Walk directories provided as arguments")
frameTime = flag.String("frame-time", "", "The amount of time between frames e.g. 10m, 5m, 30s")

buildTime string
commit string
@@ -172,6 +174,8 @@ func ProcessFile(path string) {
}
video.Meta = meta
video.Duration = meta.DurationSeconds()
video.ThumbCount = *numFrames
video.Step = ((float64(video.Duration)) / float64(video.ThumbCount))

for _, stream := range meta.Streams {
if stream.CodecType == "video" && stream.AverageFrameRate != "0/0" {
@@ -190,13 +194,24 @@ func ProcessFile(path string) {
return
}

if *frameTime != "" {
*frameTime = strings.Replace(*frameTime, " ", "", -1)
frameDuration, err := time.ParseDuration(*frameTime)
if err != nil {
log.Fatal(err)
}

video.Step = frameDuration.Seconds()
video.ThumbCount = int(video.Duration / frameDuration.Seconds())
}

if *writeInfo {
j, _ := json.MarshalIndent(video, "", " ")
ioutil.WriteFile(filepath.Join(video.GetOutputDir(), video.Filename+".json"), j, 0644)
}

generateThumbnails(&video, *numFrames)
generateContactSheet(&video, *numFrames)
generateThumbnails(&video)
generateContactSheet(&video)
}

func IsDir(path string) bool {
@@ -29,17 +29,16 @@ import (
"runtime/debug"
)

func generateThumbnails(vid *Video, numFrames int) {
func generateThumbnails(vid *Video) {
binary := GetFFMpegBinary()

if *frameWidth == 0 {
*frameWidth = vid.Width
}

for i := 0; i < numFrames; i++ {
for i := 0; i < vid.ThumbCount; i++ {
cmd := exec.Command(
binary, "-n",
"-ss", fmt.Sprintf("%f", ((float64(vid.Duration))/float64(numFrames))*float64(i)),
"-ss", fmt.Sprintf("%f", vid.Step*float64(i)),
"-i", vid.Location,
"-vframes", "1",
"-vf", fmt.Sprintf("scale=%d:-1:", *frameWidth),
@@ -28,14 +28,16 @@ import (
)

type Video struct {
Filename string
Location string
Duration float64
SHA1 sha1sum
Width int
Height int
Codec string
Meta *ffprobeOutput
Filename string
Location string
Duration float64
SHA1 sha1sum
Width int
Height int
Codec string
Meta *ffprobeOutput
Step float64
ThumbCount int
}

type sha1sum []byte

0 comments on commit d4e45c4

Please sign in to comment.
You can’t perform that action at this time.