Skip to content

Commit

Permalink
Improve code structure and set v2 package
Browse files Browse the repository at this point in the history
  • Loading branch information
xfrr committed Nov 25, 2023
1 parent 70e7953 commit c9239dd
Show file tree
Hide file tree
Showing 38 changed files with 3,426 additions and 1,890 deletions.
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
# Goffmpeg
[![Build & Test](https://github.com/xfrr/goffmpeg/actions/workflows/build_and_test.yml/badge.svg)](https://github.com/xfrr/goffmpeg/actions/workflows/build_and_test.yml)
[![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)
[![Build & Test](https://github.com/xfrr/goffmpeg/actions/workflows/build_and_test.yml/badge.svg?branch=v2)](https://github.com/xfrr/goffmpeg/actions/workflows/build_and_test.yml)
[![Code Quality](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)
[![Coverage](https://codecov.io/gh/xfrr/goffmpeg/graph/badge.svg?token=LjqrgDKO69)](https://codecov.io/gh/xfrr/goffmpeg)
[![Go Report Card](https://goreportcard.com/badge/github.com/xfrr/goffmpeg)](https://goreportcard.com/report/github.com/xfrr/goffmpeg)
[![GoDoc](https://godoc.org/github.com/xfrr/goffmpeg?status.svg)](https://godoc.org/github.com/xfrr/goffmpeg)
[![GoDoc](https://godoc.org/github.com/xfrr/goffmpeg/v2?status.svg)](https://godoc.org/github.com/xfrr/goffmpeg/v2)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE)

FFMPEG wrapper written in GO
FFMPEG wrapper written in the Go

## Features
## Supported features

- [x] Transcoding
- [x] Streaming
- [x] Progress
- [x] Filters
- [x] Thumbnails
- [x] Watermark
- [ ] Concatenation
- [ ] Subtitles
- [x] Execute ffmpeg command and get progress events
- [x] Read file and get metadata with ffprobe
- [x] Pipe Options
- [x] Global Options
- [x] Video Options
- [x] Audio Options

## Dependencies
- [FFmpeg](https://www.ffmpeg.org/)
- [FFmpeg](https://www.ffmpeg.org/) - 4.0 or higher
- [FFProbe](https://www.ffmpeg.org/ffprobe.html)

## Supported platforms
Expand All @@ -31,7 +30,7 @@ FFMPEG wrapper written in GO
## Installation
Install the package with the following command:
```shell
go get github.com/xfrr/goffmpeg
go get github.com/xfrr/goffmpeg/v2
```

## Usage
Expand Down
67 changes: 0 additions & 67 deletions config.go

This file was deleted.

18 changes: 0 additions & 18 deletions config_test.go

This file was deleted.

129 changes: 114 additions & 15 deletions examples/hls/main.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,133 @@
package main

// Execute the following script to make it work:
/*
BASE_URL=${1:-'.'}
openssl rand 16 > file.key
echo $BASE_URL/file.key > file.keyinfo
echo file.key >> file.keyinfo
echo $(openssl rand -hex 16) >> file.keyinfo
*/

import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"time"

"github.com/xfrr/goffmpeg/v2/ffmpeg"
"github.com/xfrr/goffmpeg/v2/ffmpeg/progress"
"github.com/xfrr/goffmpeg/v2/ffprobe"
)

"github.com/xfrr/goffmpeg/transcoder"
var (
keyinfoPath = flag.String("k", "file.keyinfo", "Encryption key path")
)

const (
inputPath = "../fixtures/input.3gp"
outputPath = "../test_results/hls-output.mp4"
keyinfoPath = "keyinfo"
var (
defaultInputPath = "../../testdata/input.mp4"
outputPath = flag.String("o", "../results/hls.m3u8", "output path")
)

func main() {
trans := new(transcoder.Transcoder)
flag.Parse()

inputPath := flag.Arg(0)
if inputPath == "" {
inputPath = defaultInputPath
}

err := createOutputDir(*outputPath)
if err != nil {
panic(err)
}

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

mediafile, err := ffprobe.NewCommand().
WithInputPath(inputPath).
Run(ctx)
if err != nil {
panic(err)
}

// the order of the arguments matters
cmd := ffmpeg.NewCommand().
WithInputPath(inputPath).
WithHLSKeyInfoPath(*keyinfoPath).
WithHLSSegmentTime(4).
WithOutputPath(*outputPath)

err := trans.Initialize(inputPath, outputPath)
progress, err := cmd.Start(ctx)
if err != nil {
panic(err)
}

trans.MediaFile().SetVideoCodec("libx264")
trans.MediaFile().SetHlsSegmentDuration(4)
trans.MediaFile().SetEncryptionKey(keyinfoPath)
go func() {
for msg := range progress {
printProgress(msg, mediafile.GetDuration())
}
}()

done := trans.Run(true)
progress := trans.Output()
for p := range progress {
fmt.Println(p)
err = cmd.Wait()
if err != nil {
panic(err)
}
}

func printProgress(p progress.Progress, tdur time.Duration) {
// totalDurMs is the total duration in milliseconds
tdur = time.Duration(tdur.Milliseconds()) * time.Millisecond

// rdur is the remaining duration in milliseconds
rdur := p.Duration - tdur

// convert to positive if negative
if rdur < 0 {
rdur = -rdur
}

// remainingTimeStr is the remaining time in the format 00:00:00.000
remainingTime := rdur.String()

// progress is the percentage of the progress
progress := int(float64(p.Duration.Milliseconds()) / float64(tdur.Milliseconds()) * 100)

fmt.Printf(`
Progress:
- Current Time: %s
- Remaining Time: %s
- Progress: %d%s
- Frames Processed: %d
- Current Bitrate: %f
- Size: %d
- Speed: %f
- Fps: %f
- Dup: %d
- Drop: %d
`,
p.Duration,
remainingTime,
progress, "%",
p.FramesProcessed,
p.Bitrate,
p.Size,
p.Speed,
p.Fps,
p.Dup,
p.Drop,
)
}

func createOutputDir(outputPath string) error {
err := os.MkdirAll(filepath.Dir(outputPath), 0755)
if err != nil {
return err
}

fmt.Println(<-done)
return nil
}
Loading

0 comments on commit c9239dd

Please sign in to comment.