From f764517845000f78fafa705ed9507cf8591dca4e Mon Sep 17 00:00:00 2001 From: Fran Date: Fri, 3 Apr 2020 13:28:23 +0200 Subject: [PATCH] Fix ffmpeg and ffprobe path, minor fixes --- LICENSE | 2 +- README.md | 540 +++++++++++++++++++++++------------------------ ffmpeg/ffmpeg.go | 6 +- 3 files changed, 274 insertions(+), 274 deletions(-) diff --git a/LICENSE b/LICENSE index 7ba1c1b..f21a1ef 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Fran +Copyright (c) 2018 FlooStack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 159836d..71763b5 100644 --- a/README.md +++ b/README.md @@ -1,270 +1,270 @@ -# Goffmpeg -[![Codacy Badge](https://api.codacy.com/project/badge/Grade/93e018e5008b4439acbb30d715b22e7f)](https://www.codacy.com/app/francisco.romero/goffmpeg?utm_source=github.com&utm_medium=referral&utm_content=xfrr/goffmpeg&utm_campaign=Badge_Grade) - -FFMPEG wrapper written in GO which allows to obtain the progress. - -# Dependencies -- [FFmpeg](https://www.ffmpeg.org/) -- [FFProbe](https://www.ffmpeg.org/ffprobe.html) - -# Supported platforms - - - Linux - - OS X - - Windows - -# Getting started -## How to transcode a media file -```shell -go get github.com/xfrr/goffmpeg -``` - -```go -package main - -import ( - "github.com/xfrr/goffmpeg/transcoder" -) - -var inputPath = "/data/testmov" -var outputPath = "/data/testmp4.mp4" - -func main() { - - // Create new instance of transcoder - trans := new(transcoder.Transcoder) - - // Initialize transcoder passing the input file path and output file path - err := trans.Initialize( inputPath, outputPath ) - // Handle error... - - // Start transcoder process without checking progress - done := trans.Run(false) - - // This channel is used to wait for the process to end - err = <-done - // Handle error... - -} -``` -## How to get the transcoding progress -```go -... -func main() { - - // Create new instance of transcoder - trans := new(transcoder.Transcoder) - - // Initialize transcoder passing the input file path and output file path - err := trans.Initialize( inputPath, outputPath ) - // Handle error... - - // Start transcoder process with progress checking - done := trans.Run(true) - - // Returns a channel to get the transcoding progress - progress := trans.Output() - - // Example of printing transcoding progress - for msg := range progress { - fmt.Println(msg) - } - - // This channel is used to wait for the transcoding process to end - err = <-done - -} -``` - -## How to pipe in data using the [pipe protocol](https://ffmpeg.org/ffmpeg-protocols.html#pipe) -Creating an input pipe will return [\*io.PipeReader](https://golang.org/pkg/io/#PipeReader), and creating an output pipe will return [\*io.PipeWriter](https://golang.org/pkg/io/#PipeWriter). An example is shown which uses `cat` to pipe in data, and [ioutil.ReadAll](https://golang.org/pkg/io/ioutil/#ReadAll) to read data as bytes from the pipe. -```go -func main() { - - // Create new instance of transcoder - trans := new(transcoder.Transcoder) - - // Initialize an empty transcoder - err := trans.InitializeEmptyTranscoder() - // Handle error... - - // Create a command such that its output should be passed as stdin to ffmpeg - cmd := exec.Command("cat", "/path/to/file") - - // Create an input pipe to write to, which will return *io.PipeWriter - w, err := trans.CreateInputPipe() - - cmd.Stdout = w - - // Create an output pipe to read from, which will return *io.PipeReader. - // Must also specify the output container format - r, err := trans.CreateOutputPipe("mp4") - - wg := &sync.WaitGroup{} - wg.Add(1) - go func() { - defer r.Close() - defer wg.Done() - - // Read data from output pipe - data, err := ioutil.ReadAll(r) - // Handle error and data... - }() - - go func() { - defer w.Close() - err := cmd.Run() - // Handle error... - }() - - // Start transcoder process without checking progress - done := trans.Run(false) - - // This channel is used to wait for the transcoding process to end - err = <-done - // Handle error... - - wg.Wait() -} -``` - -# Progress properties -```go -type Progress struct { - FramesProcessed string - CurrentTime string - CurrentBitrate string - Progress float64 - Speed string -} -``` -# Media setters -Those options can be set before starting the transcoding. -```js -SetAspect -SetResolution -SetVideoBitRate -SetVideoBitRateTolerance -SetVideoMaxBitrate -SetVideoMinBitRate -SetVideoCodec -SetVframes -SetFrameRate -SetAudioRate -SetSkipAudio -SetSkipVideo -SetMaxKeyFrame -SetMinKeyFrame -SetKeyframeInterval -SetAudioCodec -SetAudioBitRate -SetAudioChannels -SetBufferSize -SetThreads -SetPreset -SetTune -SetAudioProfile -SetVideoProfile -SetDuration -SetDurationInput -SetSeekTime -SetSeekTimeInput -SetSeekUsingTsInput -SetQuality -SetStrict -SetCopyTs -SetMuxDelay -SetHideBanner -SetInputPath -SetNativeFramerateInput -SetRtmpLive -SetHlsListSize -SetHlsSegmentDuration -SetHlsPlaylistType -SetHlsMasterPlaylistName -SetHlsSegmentFilename -SetHttpMethod -SetHttpKeepAlive -SetOutputPath -SetOutputFormat -SetAudioFilter -SetAudioVariableBitrate -SetCompressionLevel -SetFilter -SetInputInitialOffset -SetInputPipeCommand -SetMapMetadata -SetMetadata -SetStreamIds -SetTags -SetVideoFilter -``` -Example -```golang -func main() { - - // Create new instance of transcoder - trans := new(transcoder.Transcoder) - - // Initialize transcoder passing the input file path and output file path - err := trans.Initialize( inputPath, outputPath ) - // Handle error... - - // SET FRAME RATE TO MEDIAFILE - trans.MediaFile().SetFrameRate(70) - // SET ULTRAFAST PRESET TO MEDIAFILE - trans.MediaFile().SetPreset("ultrafast") - - // Start transcoder process to check progress - done := trans.Run(true) - - // Returns a channel to get the transcoding progress - progress := trans.Output() - - // Example of printing transcoding progress - for msg := range progress { - fmt.Println(msg) - } - - // This channel is used to wait for the transcoding process to end - err = <-done - -} -``` - -Example with AES encryption : - -More information about [HLS encryption with FFMPEG](https://hlsbook.net/how-to-encrypt-hls-video-with-ffmpeg/) - -```bash -# Generate key -openssl rand 16 > enc.key -``` - -Create key file info : - -```enc.keyinfo -Key URI -Path to key file -``` - -```golang -func main() { - - trans := new(transcoder.Transcoder) - - err := trans.Initialize(inputPath, outputPath) - - trans.MediaFile().SetVideoCodec("libx264") - - trans.MediaFile().SetHlsSegmentDuration(4) - - trans.MediaFile().SetEncryptionKey(keyinfoPath) - - progress := trans.Output() - - err = <-done -} - ----- -> Building... +# Goffmpeg +[![Codacy Badge](https://api.codacy.com/project/badge/Grade/93e018e5008b4439acbb30d715b22e7f)](https://www.codacy.com/app/francisco.romero/goffmpeg?utm_source=github.com&utm_medium=referral&utm_content=xfrr/goffmpeg&utm_campaign=Badge_Grade) + +FFMPEG wrapper written in GO which allows to obtain the progress. + +# Dependencies +- [FFmpeg](https://www.ffmpeg.org/) +- [FFProbe](https://www.ffmpeg.org/ffprobe.html) + +# Supported platforms + + - Linux + - OS X + - Windows + +# Getting started +## How to transcode a media file +```shell +go get github.com/xfrr/goffmpeg +``` + +```go +package main + +import ( + "github.com/xfrr/goffmpeg/transcoder" +) + +var inputPath = "/data/testmov" +var outputPath = "/data/testmp4.mp4" + +func main() { + + // Create new instance of transcoder + trans := new(transcoder.Transcoder) + + // Initialize transcoder passing the input file path and output file path + err := trans.Initialize( inputPath, outputPath ) + // Handle error... + + // Start transcoder process without checking progress + done := trans.Run(false) + + // This channel is used to wait for the process to end + err = <-done + // Handle error... + +} +``` +## How to get the transcoding progress +```go +... +func main() { + + // Create new instance of transcoder + trans := new(transcoder.Transcoder) + + // Initialize transcoder passing the input file path and output file path + err := trans.Initialize( inputPath, outputPath ) + // Handle error... + + // Start transcoder process with progress checking + done := trans.Run(true) + + // Returns a channel to get the transcoding progress + progress := trans.Output() + + // Example of printing transcoding progress + for msg := range progress { + fmt.Println(msg) + } + + // This channel is used to wait for the transcoding process to end + err = <-done + +} +``` + +## How to pipe in data using the [pipe protocol](https://ffmpeg.org/ffmpeg-protocols.html#pipe) +Creating an input pipe will return [\*io.PipeReader](https://golang.org/pkg/io/#PipeReader), and creating an output pipe will return [\*io.PipeWriter](https://golang.org/pkg/io/#PipeWriter). An example is shown which uses `cat` to pipe in data, and [ioutil.ReadAll](https://golang.org/pkg/io/ioutil/#ReadAll) to read data as bytes from the pipe. +```go +func main() { + + // Create new instance of transcoder + trans := new(transcoder.Transcoder) + + // Initialize an empty transcoder + err := trans.InitializeEmptyTranscoder() + // Handle error... + + // Create a command such that its output should be passed as stdin to ffmpeg + cmd := exec.Command("cat", "/path/to/file") + + // Create an input pipe to write to, which will return *io.PipeWriter + w, err := trans.CreateInputPipe() + + cmd.Stdout = w + + // Create an output pipe to read from, which will return *io.PipeReader. + // Must also specify the output container format + r, err := trans.CreateOutputPipe("mp4") + + wg := &sync.WaitGroup{} + wg.Add(1) + go func() { + defer r.Close() + defer wg.Done() + + // Read data from output pipe + data, err := ioutil.ReadAll(r) + // Handle error and data... + }() + + go func() { + defer w.Close() + err := cmd.Run() + // Handle error... + }() + + // Start transcoder process without checking progress + done := trans.Run(false) + + // This channel is used to wait for the transcoding process to end + err = <-done + // Handle error... + + wg.Wait() +} +``` + +# Progress properties +```go +type Progress struct { + FramesProcessed string + CurrentTime string + CurrentBitrate string + Progress float64 + Speed string +} +``` +# Media setters +Those options can be set before starting the transcoding. +```js +SetAspect +SetResolution +SetVideoBitRate +SetVideoBitRateTolerance +SetVideoMaxBitrate +SetVideoMinBitRate +SetVideoCodec +SetVframes +SetFrameRate +SetAudioRate +SetSkipAudio +SetSkipVideo +SetMaxKeyFrame +SetMinKeyFrame +SetKeyframeInterval +SetAudioCodec +SetAudioBitRate +SetAudioChannels +SetBufferSize +SetThreads +SetPreset +SetTune +SetAudioProfile +SetVideoProfile +SetDuration +SetDurationInput +SetSeekTime +SetSeekTimeInput +SetSeekUsingTsInput +SetQuality +SetStrict +SetCopyTs +SetMuxDelay +SetHideBanner +SetInputPath +SetNativeFramerateInput +SetRtmpLive +SetHlsListSize +SetHlsSegmentDuration +SetHlsPlaylistType +SetHlsMasterPlaylistName +SetHlsSegmentFilename +SetHttpMethod +SetHttpKeepAlive +SetOutputPath +SetOutputFormat +SetAudioFilter +SetAudioVariableBitrate +SetCompressionLevel +SetFilter +SetInputInitialOffset +SetInputPipeCommand +SetMapMetadata +SetMetadata +SetStreamIds +SetTags +SetVideoFilter +``` +Example +```golang +func main() { + + // Create new instance of transcoder + trans := new(transcoder.Transcoder) + + // Initialize transcoder passing the input file path and output file path + err := trans.Initialize( inputPath, outputPath ) + // Handle error... + + // SET FRAME RATE TO MEDIAFILE + trans.MediaFile().SetFrameRate(70) + // SET ULTRAFAST PRESET TO MEDIAFILE + trans.MediaFile().SetPreset("ultrafast") + + // Start transcoder process to check progress + done := trans.Run(true) + + // Returns a channel to get the transcoding progress + progress := trans.Output() + + // Example of printing transcoding progress + for msg := range progress { + fmt.Println(msg) + } + + // This channel is used to wait for the transcoding process to end + err = <-done + +} +``` + +Example with AES encryption : + +More information about [HLS encryption with FFMPEG](https://hlsbook.net/how-to-encrypt-hls-video-with-ffmpeg/) + +```bash +# Generate key +openssl rand 16 > enc.key +``` + +Create key file info : + +```enc.keyinfo +Key URI +Path to key file +``` + +```golang +func main() { + + trans := new(transcoder.Transcoder) + + err := trans.Initialize(inputPath, outputPath) + + trans.MediaFile().SetVideoCodec("libx264") + + trans.MediaFile().SetHlsSegmentDuration(4) + + trans.MediaFile().SetEncryptionKey(keyinfoPath) + + progress := trans.Output() + + err = <-done +} + +---- +> Building... diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index e6866ae..89292e8 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -31,9 +31,9 @@ func Configure() (Configuration, error) { return Configuration{}, err } - ffmpeg := strings.Replace(outFFmpeg.String(), utils.LineSeparator(), "", -1) - fprobe := strings.Replace(outProbe.String(), utils.LineSeparator(), "", -1) + ffmpeg := strings.Replace(strings.Split(outFFmpeg.String(), "\n")[0], utils.LineSeparator(), "", -1) + ffprobe := strings.Replace(strings.Split(outProbe.String(), "\n")[0], utils.LineSeparator(), "", -1) - cnf := Configuration{ffmpeg, fprobe} + cnf := Configuration{ffmpeg, ffprobe} return cnf, nil }