giffer

Create .gif images from sites like youtube.com
Log | Files | Refs | LICENSE

commit 70eea89b7d61336516ed048f014aaf4b171d8973
parent c3243d2235202658bcc2b7c0a366a57c90a0fd97
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date:   Wed, 21 Nov 2018 00:14:41 +1300

[~] Better naming and configurable logging.

Diffstat:
Mcmd/cli/main.go | 13+++++++++----
Mcmd/desktop/giffer.go | 2+-
Mcmd/desktop/main.go | 7++++---
Mdownload.go | 12+++++++++++-
Dffmpeg.go | 71-----------------------------------------------------------------------
Atranscode.go | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 104 insertions(+), 80 deletions(-)

diff --git a/cmd/cli/main.go b/cmd/cli/main.go @@ -48,7 +48,10 @@ func main() { videofile = "tmp" } else if url != "" { dl := giffer.Downloader{ - Dir: "./tmp/dl", + Dir: "./tmp/dl", + FFmpeg: "ffmpeg", + Debug: debug, + Out: os.Stdout, } downloaded, err := dl.Download(url, 0, 0, giffer.Medium) if err != nil { @@ -56,10 +59,12 @@ func main() { } videofile = downloaded } - ffmpeg := giffer.FFMpeg{ - Debug: debug, + t := giffer.Transcoder{ + FFmpeg: "ffmpeg", + Debug: debug, + Out: os.Stdout, } - gif, err := ffmpeg.Convert(videofile, fps, width, height, "gif", "gif") + gif, err := t.Convert(videofile, fps, width, height, "gif", "gif") if err != nil { log.Fatalf("converting to gif: %v", err) } diff --git a/cmd/desktop/giffer.go b/cmd/desktop/giffer.go @@ -15,7 +15,7 @@ import ( // Giffer wraps the giffer business logic. type Giffer struct { *giffer.Downloader - *giffer.FFMpeg + *giffer.Transcoder Store GifStore } diff --git a/cmd/desktop/main.go b/cmd/desktop/main.go @@ -75,12 +75,13 @@ func main() { App: &Giffer{ Downloader: &giffer.Downloader{ Dir: filepath.Join(filepath.Dir(ffmpeg), "tmp/downloads"), + Debug: verbose, FFmpeg: ffmpeg, Out: logf, }, - FFMpeg: &giffer.FFMpeg{ - Debug: verbose, - Use: ffmpeg, + Transcoder: &giffer.Transcoder{ + Debug: verbose, + FFmpeg: ffmpeg, }, Store: &gifdb{ Dir: filepath.Join(filepath.Dir(ffmpeg), "tmp/gifs"), diff --git a/download.go b/download.go @@ -38,6 +38,7 @@ import ( type Downloader struct { Dir string FFmpeg string + Debug bool Out io.Writer } @@ -48,8 +49,10 @@ func (dl Downloader) Download( start, end float64, q Quality, ) (string, error) { + // Side channel for loading config because of how the package is + // unfortunately structured. config.FFmpeg = dl.FFmpeg - fmt.Fprintf(dl.Out, "ffmpeg: %q\n", dl.FFmpeg) + dl.logf("ffmpeg: %q\n", dl.FFmpeg) input := fmt.Sprintf("%s_%f_%f_%s", URL, start, end, q) h, err := hash(input) if err != nil { @@ -83,6 +86,13 @@ func (dl Downloader) Download( return real, nil } +func (dl Downloader) logf(f string, v ...interface{}) { + if !dl.Debug || dl.Out == nil { + return + } + fmt.Fprintf(dl.Out, f, v...) +} + // Quality is an enum representing the various video qualities. type Quality int diff --git a/ffmpeg.go b/ffmpeg.go @@ -1,71 +0,0 @@ -package giffer - -import ( - "bytes" - "fmt" - "log" - "strings" - - "os/exec" -) - -// FFMpeg wraps the ffmpeg binary. -type FFMpeg struct { - // Use is a path to an ffmpeg binary. - // If empty, system path is used. - Use string - // Debug logs the ffmpeg command. - Debug bool -} - -// Convert a video into that of the specified encoding and format between start -// and end. -// If end is zero we convert from start until the end of the video. -func (f FFMpeg) Convert( - video string, - fps float64, - width, height int, - encoding, format string, -) (*bytes.Buffer, error) { - var ( - out bytes.Buffer - args []string - bin = "ffmpeg" - ) - if f.Use != "" { - bin = f.Use - } - args = append(args, "-i", video) - if width > 0 || height > 0 || fps > 0 { - var vfargs []string - if fps > 0 { - vfargs = append(vfargs, fmt.Sprintf("fps=%2f", fps)) - } - if width > 0 || height > 0 { - if width <= 0 { - width = -1 - } - if height <= 0 { - height = -1 - } - vfargs = append(vfargs, fmt.Sprintf("scale=%d:%d", width, height)) - } - args = append(args, "-vf", strings.Join(vfargs, ",")) - } - args = append(args, - "-c", "copy", - "-c:v", encoding, - "-f", format, "-", - ) - if f.Debug { - log.Printf("%s %s", bin, strings.Join(args, " ")) - } - cmd := CmdPipe{ - Out: &out, - Debug: f.Debug, - Stack: []*exec.Cmd{ - exec.Command(bin, args...), - }, - } - return &out, cmd.Run() -} diff --git a/transcode.go b/transcode.go @@ -0,0 +1,79 @@ +package giffer + +import ( + "bytes" + "fmt" + "io" + "strings" + + "os/exec" +) + +// Transcoder converts video files to Gif images by wrappping FFmpeg. +type Transcoder struct { + // FFmpeg is a path to an FFmpeg binary. + // If empty, system path is used. + FFmpeg string + // Debug logs the FFmpeg command. + Debug bool + Out io.Writer +} + +// Convert a video into that of the specified encoding and format between start +// and end. +// If end is zero we convert from start until the end of the video. +func (t Transcoder) Convert( + video string, + fps float64, + width, height int, + encoding, format string, +) (*bytes.Buffer, error) { + var ( + out bytes.Buffer + args []string + bin = "ffmpeg" + ) + if t.FFmpeg != "" { + bin = t.FFmpeg + } + args = append(args, "-i", video) + if width > 0 || height > 0 || fps > 0 { + var vfargs []string + if fps > 0 { + vfargs = append(vfargs, fmt.Sprintf("fps=%2f", fps)) + } + if width > 0 || height > 0 { + if width <= 0 { + width = -1 + } + if height <= 0 { + height = -1 + } + vfargs = append(vfargs, fmt.Sprintf("scale=%d:%d", width, height)) + } + args = append(args, "-vf", strings.Join(vfargs, ",")) + } + args = append(args, + "-c", "copy", + "-c:v", encoding, + "-f", format, "-", + ) + if t.Debug { + t.logf("%s %s", bin, strings.Join(args, " ")) + } + cmd := CmdPipe{ + Out: &out, + Debug: t.Debug, + Stack: []*exec.Cmd{ + exec.Command(bin, args...), + }, + } + return &out, cmd.Run() +} + +func (t Transcoder) logf(f string, v ...interface{}) { + if !t.Debug || t.Out == nil { + return + } + fmt.Fprintf(t.Out, f, v...) +}