Giter Site home page Giter Site logo

goffmpeg's Introduction

Goffmpeg

Codacy Badge

FFMPEG wrapper written in GO which allows to obtain the progress.

V2

New implementation with an easy-to-use API and interfaces to extend the transcoding capabilities.

https://github.com/floostack/transcoder

Dependencies

Supported platforms

  • Linux
  • OS X
  • Windows

Getting started

How to transcode a media file

go get github.com/xfrr/goffmpeg
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

...
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

Creating an input pipe will return *io.PipeReader, and creating an output pipe will return *io.PipeWriter. An example is shown which uses cat to pipe in data, and ioutil.ReadAll to read data as bytes from the pipe.

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

type Progress struct {
	FramesProcessed string
	CurrentTime     string
	CurrentBitrate  string
	Progress        float64
	Speed           string
}

Media setters

Those options can be set before starting the transcoding.

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
SetSingleFile
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

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

# Generate key
openssl rand 16 > enc.key

Create key file info :

Key URI
Path to key file
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
}

goffmpeg's People

Contributors

xfrr avatar muhammadharis avatar deloz avatar rohitggarg avatar r3b-fish avatar timotto avatar arcayr avatar recurser avatar mie00 avatar basriciftci avatar nks avatar tarrencev avatar agxp avatar ggymm avatar manscrober avatar vivienbcr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.