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