Skip to content

Commit

Permalink
Refactor media file and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
xfrr committed Nov 25, 2023
1 parent 6fd02b1 commit bc1ad83
Show file tree
Hide file tree
Showing 13 changed files with 634 additions and 168 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ build:
go build -ldflags="-s -w" -o ./bin/ ./...

.PHONY: coverage-html
coverage-html:
open-coverage-html:
go tool cover -html=coverage.out

.PHONY: coverage-total
coverage-total:
print-total-coverage:
go tool cover -func=coverage.out | grep total | awk '{print $$3}'
49 changes: 22 additions & 27 deletions examples/hls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func main() {

go func() {
for msg := range progress {
printProgress(msg, mediafile.GetDuration())
printProgress(msg, mediafile.Duration())
}
}()

Expand All @@ -80,28 +80,23 @@ func main() {
}

func printProgress(p progress.Progress, tdur time.Duration) {
// totalDurMs is the total duration in milliseconds
tdur = time.Duration(tdur.Milliseconds()) * time.Millisecond
// calculate the progress percentage
progressPct := fmt.Sprintf("%d%s", p.Duration().Milliseconds()/tdur.Milliseconds()*100, "%")

// rdur is the remaining duration in milliseconds
rdur := p.Duration - tdur
// convert the total duration to milliseconds
totalDuration := time.Duration(tdur.Milliseconds()) * time.Millisecond

// convert to positive if negative
if rdur < 0 {
rdur = -rdur
// calculate the remaining duration
remainingDuration := p.Duration() - totalDuration
if remainingDuration < 0 {
remainingDuration = -remainingDuration
}

// 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
- Progress: %s
- Time Remaining: %s
- Time Processed: %s
- Frames Processed: %d
- Current Bitrate: %f
- Size: %d
Expand All @@ -110,16 +105,16 @@ Progress:
- Dup: %d
- Drop: %d
`,
p.Duration,
remainingTime,
progress, "%",
p.FramesProcessed,
p.Bitrate,
p.Size,
p.Speed,
p.Fps,
p.Dup,
p.Drop,
progressPct,
remainingDuration,
p.Duration(),
p.FramesProcessed(),
p.Bitrate(),
p.Size(),
p.Speed(),
p.Fps(),
p.Dup(),
p.Drop(),
)
}

Expand Down
49 changes: 22 additions & 27 deletions examples/pipes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func main() {

go func() {
for msg := range progress {
printProgress(msg, mediafile.GetDuration())
printProgress(msg, mediafile.Duration())
}
}()
}()
Expand All @@ -78,28 +78,23 @@ func main() {
}

func printProgress(p progress.Progress, tdur time.Duration) {
// totalDurMs is the total duration in milliseconds
tdur = time.Duration(tdur.Milliseconds()) * time.Millisecond
// calculate the progress percentage
progressPct := fmt.Sprintf("%d%s", p.Duration().Milliseconds()/tdur.Milliseconds()*100, "%")

// rdur is the remaining duration in milliseconds
rdur := p.Duration - tdur
// convert the total duration to milliseconds
totalDuration := time.Duration(tdur.Milliseconds()) * time.Millisecond

// convert to positive if negative
if rdur < 0 {
rdur = -rdur
// calculate the remaining duration
remainingDuration := p.Duration() - totalDuration
if remainingDuration < 0 {
remainingDuration = -remainingDuration
}

// 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
- Progress: %s
- Time Remaining: %s
- Time Processed: %s
- Frames Processed: %d
- Current Bitrate: %f
- Size: %d
Expand All @@ -108,16 +103,16 @@ Progress:
- Dup: %d
- Drop: %d
`,
p.Duration,
remainingTime,
progress, "%",
p.FramesProcessed,
p.Bitrate,
p.Size,
p.Speed,
p.Fps,
p.Dup,
p.Drop,
progressPct,
remainingDuration,
p.Duration(),
p.FramesProcessed(),
p.Bitrate(),
p.Size(),
p.Speed(),
p.Fps(),
p.Dup(),
p.Drop(),
)
}

Expand Down
21 changes: 10 additions & 11 deletions examples/rmtp-stream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,23 @@ func main() {
func printProgress(p progress.Progress) {
fmt.Printf(`
Progress:
- Time Streamed: %s
- Time Processed: %s
- Frames Processed: %d
- Bitrate: %f
- Current Bitrate: %f
- Size: %d
- Speed: %f
- Fps: %f
- Dup: %d
- Drop: %d
`,
p.Duration,
p.FramesProcessed,
p.Bitrate,
p.Size,
p.Speed,
p.Fps,
p.Dup,
p.Drop,
p.Duration(),
p.FramesProcessed(),
p.Bitrate(),
p.Size(),
p.Speed(),
p.Fps(),
p.Dup(),
p.Drop(),
)
}

Expand Down
21 changes: 10 additions & 11 deletions examples/rtsp-stream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,23 @@ func main() {
func printProgress(p progress.Progress) {
fmt.Printf(`
Progress:
- Time Recorded: %s
- Time Processed: %s
- Frames Processed: %d
- Bitrate: %f
- Current Bitrate: %f
- Size: %d
- Speed: %f
- Fps: %f
- Dup: %d
- Drop: %d
`,
p.Duration,
p.FramesProcessed,
p.Bitrate,
p.Size,
p.Speed,
p.Fps,
p.Dup,
p.Drop,
p.Duration(),
p.FramesProcessed(),
p.Bitrate(),
p.Size(),
p.Speed(),
p.Fps(),
p.Dup(),
p.Drop(),
)
}

Expand Down
48 changes: 40 additions & 8 deletions ffmpeg/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,44 @@ package progress
import "time"

type Progress struct {
FramesProcessed int64
Bitrate float64
Speed float64
Size int64
Fps float64
Drop int64
Dup int64
Duration time.Duration
framesProcessed int64
bitrate float64
speed float64
size int64
fps float64
drop int64
dup int64
duration time.Duration
}

func (p Progress) FramesProcessed() int64 {
return p.framesProcessed
}

func (p Progress) Bitrate() float64 {
return p.bitrate
}

func (p Progress) Speed() float64 {
return p.speed
}

func (p Progress) Size() int64 {
return p.size
}

func (p Progress) Fps() float64 {
return p.fps
}

func (p Progress) Drop() int64 {
return p.drop
}

func (p Progress) Dup() int64 {
return p.dup
}

func (p Progress) Duration() time.Duration {
return p.duration
}
32 changes: 16 additions & 16 deletions ffmpeg/progress/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,66 +54,66 @@ func (dr *reader) Read(ctx context.Context, r io.Reader, progress chan<- Progres

line = cleanLine(line)

if p.FramesProcessed == 0 {
if p.framesProcessed == 0 {
fp := parseInt64("frame", line)
if fp != 0 {
p.FramesProcessed = fp
p.framesProcessed = fp
continue
}
}

if p.Fps == 0 {
if p.fps == 0 {
fps := parseFloat64("fps", line)
if fps != 0 {
p.Fps = fps
p.fps = fps
continue
}
}

if p.Bitrate == 0 {
if p.bitrate == 0 {
bitrate := parseFloat64("bitrate", line)
if bitrate != 0 {
p.Bitrate = bitrate
p.bitrate = bitrate
continue
}
}

if p.Size == 0 {
if p.size == 0 {
size := parseInt64("total_size", line)
if size != 0 {
p.Size = size
p.size = size
continue
}
}

if p.Duration == 0 {
if p.duration == 0 {
duration := parseDurationMs("out_time_ms", line)
if duration != 0 {
p.Duration = duration
p.duration = duration
continue
}
}

if p.Drop == 0 {
if p.drop == 0 {
drop := parseInt64("drop_frames", line)
if drop != 0 {
p.Drop = drop
p.drop = drop
continue
}
}

if p.Dup == 0 {
if p.dup == 0 {
dup := parseInt64("dup_frames", line)
if dup != 0 {
p.Dup = dup
p.dup = dup
continue
}
}

if p.Speed == 0 {
if p.speed == 0 {
speed := parseFloat64("speed", line)
if speed != 0 {
p.Speed = speed
p.speed = speed
continue
}
}
Expand Down
Loading

0 comments on commit bc1ad83

Please sign in to comment.