Something went wrong. Try again.
A concatenating Youtube uploader
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143package pkg
import ( "fmt" "io" "strings" "sync" "time")
const ( progressWidth = 30 renderInterval = time.Second minRateElapsed = 2 * time.Second)
// ProgressReader counts the bytes read from an underlying reader and renders a// progress bar. The total is an estimate (the sum of the input file sizes);// MPEG-TS overhead means the number of bytes read can exceed it.type ProgressReader struct { r io.Reader total int64 out io.Writer
mu sync.Mutex start time.Time read int64 lastPercent int64 lastRender time.Time lastLen int rendered bool}
// NewProgressReader wraps r and renders progress to out. A non-positive total// is treated as unknown.func NewProgressReader(r io.Reader, total int64, out io.Writer) *ProgressReader { return &ProgressReader{r: r, total: total, out: out, start: time.Now(), lastPercent: -1}}
// Read reads from the underlying reader and updates the progress display.func (p *ProgressReader) Read(b []byte) (int, error) { n, err := p.r.Read(b)
p.mu.Lock() p.read += int64(n) p.renderLocked() p.mu.Unlock()
return n, err}
// Finish completes the progress line. It should be called once no more reads// are expected.func (p *ProgressReader) Finish() { p.mu.Lock() defer p.mu.Unlock()
if !p.rendered { return }
p.lastPercent = -1 p.renderLocked() _, _ = fmt.Fprintln(p.out)}
func (p *ProgressReader) renderLocked() { now := time.Now() read := p.read percent := int64(0)
if p.total > 0 { percent = min(read*100/p.total, 100) }
if percent == p.lastPercent && now.Sub(p.lastRender) < renderInterval { return } p.lastPercent = percent p.lastRender = now p.rendered = true
displayRead := read if p.total > 0 && displayRead > p.total { displayRead = p.total }
filled := int(percent) * progressWidth / 100 bar := strings.Repeat("=", filled) + strings.Repeat(" ", progressWidth-filled)
var line string if p.total > 0 { line = fmt.Sprintf("[%s] %3d%% %s / %s", bar, percent, formatBytes(displayRead), formatBytes(p.total)) } else { line = fmt.Sprintf("[%s] %s", bar, formatBytes(displayRead)) }
elapsed := now.Sub(p.start) if elapsed >= minRateElapsed { rate := float64(read) / elapsed.Seconds() line += fmt.Sprintf(" %s/s", formatBytes(int64(rate)))
if p.total > 0 && read < p.total { line += " ETA " + formatETA(p.total-read, rate) } }
// Carriage return moves the cursor but does not erase, so pad the line to // the width of the previous one. Otherwise a shorter line (rate and ETA // shrink all the time) leaves stale characters at the end. if pad := p.lastLen - len(line); pad > 0 { line += strings.Repeat(" ", pad) } p.lastLen = len(line)
_, _ = fmt.Fprintf(p.out, "\r%s", line)}
func formatETA(remaining int64, rate float64) string { if remaining <= 0 || rate <= 0 { return "?" }
seconds := float64(remaining) / rate return time.Duration(seconds * float64(time.Second)).Round(time.Second).String()}
func formatBytes(n int64) string { const unit = 1024
if n < unit { return fmt.Sprintf("%d B", n) }
div, exp := int64(unit), 0 for m := n / unit; m >= unit; m /= unit { div *= unit exp++ }
return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp])}