Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Use direct data copying to combine chunks
Browse files Browse the repository at this point in the history
Previous approach was causing problems with longer video downloads due
to the size of the command/number of files passed to ffmpeg. This
approach is to sequentially iterate the downloaded chunks and directly
write their content to the output file and now shows a progress bar due
to ease of implementation.
  • Loading branch information
dbarbuzzi committed Jan 8, 2021
1 parent 5949a04 commit a919146
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions tvd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
// Based on https://github.com/ArneVogel/concat

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand All @@ -12,10 +11,8 @@ import (
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"

Expand Down Expand Up @@ -452,27 +449,27 @@ func buildOutFilePath(vodID int, startAt int, dur int, prefix string, folder str
}

func combineChunks(chunks []Chunk, outfile string) error {
ffmpeg := "ffmpeg"
if runtime.GOOS == "windows" {
ffmpeg += ".exe"
of, err := os.Create(outfile)
if err != nil {
return err
}
log.Printf("ffmpeg command: %s\n", ffmpeg)
defer of.Close()

concat := "concat:"
bar := pb.StartNew(len(chunks))
for _, c := range chunks {
concat += c.Path + "|"
}
concat = string(concat[0 : len(concat)-1])
cf, err := os.Open(c.Path)
if err != nil {
return err
}

args := []string{"-i", concat, "-c", "copy", "-bsf:a", "aac_adtstoasc", "-fflags", "+genpts", outfile}
log.Printf("ffmpeg args:\n%+v\n", args)
cmd := exec.Command(ffmpeg, args...)
var errbuf bytes.Buffer
cmd.Stderr = &errbuf
err := cmd.Run()
if err != nil {
return fmt.Errorf(errbuf.String())
_, err = io.Copy(of, cf)
cf.Close()
if err != nil {
return err
}
bar.Increment()
}
bar.Finish()

return nil
}
Expand Down

0 comments on commit a919146

Please sign in to comment.