-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve code structure and set v2 package
- Loading branch information
Showing
38 changed files
with
3,426 additions
and
1,890 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.