Skip to content

Commit

Permalink
- [#] implemented option -S,Seg -- split video into multiple segments
Browse files Browse the repository at this point in the history
  • Loading branch information
suntong committed Oct 10, 2021
1 parent 25c6230 commit f8b25e1
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions ffcvt.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ func main() {
os.Exit(0)
}

if len(Opts.Seg) > 0 {
// sanity check
_, err := time.Parse("15:04:05", Opts.Seg)
if err != nil {
fmt.Fprintf(os.Stderr, "Seg format error: '%s'\n", Opts.Seg)
os.Exit(1)
}
}

if len(Opts.Cut) > 0 {
var b, vc strings.Builder
var ci int
Expand Down Expand Up @@ -264,7 +273,10 @@ func transcodeVideos(startTime time.Time) {

func transcodeFile(inputName string) {
startTime := time.Now()
outputName := getOutputName(inputName)
outputName, outputGrpName := getOutputName(inputName), ""
if len(Opts.Seg) > 0 {
outputName, outputGrpName = getOutputNameSeg(inputName)
}
debug(outputName, 4)
os.MkdirAll(filepath.Dir(outputName), os.ModePerm)
var oldAEP, oldVEP, oldSEP string
Expand Down Expand Up @@ -319,6 +331,14 @@ func transcodeFile(inputName string) {
if Opts.Force {
args = append(args, "-y")
}
if len(Opts.Seg) > 0 {
args = append(args, "-f")
args = append(args, "segment")
args = append(args, "-segment_time")
args = append(args, Opts.Seg)
args = append(args, "-reset_timestamps")
args = append(args, "1")
}
if len(cutOps) != 0 {
args = append(args, "-filter_complex")
args = append(args, cutOps)
Expand All @@ -328,7 +348,11 @@ func transcodeFile(inputName string) {
args = append(args, "[ao]")
}
args = append(args, flag.Args()...)
args = append(args, outputName)
if len(Opts.Seg) > 0 {
args = append(args, outputGrpName)
} else {
args = append(args, outputName)
}
debug(Opts.FFMpeg+" "+strings.Join(args, " "), 1)

if Opts.NoExec {
Expand Down Expand Up @@ -529,6 +553,16 @@ func getOutputName(input string) string {
return r
}

// getOutputNameSeg will do getOutputName() but tailored toward video segmenting.
// The first return will be the first video segment file name, 00_.mkv, while
// the second return will be the segment group file name, %02d_.mkv
func getOutputNameSeg(input string) (string, string) {
r := getOutputName(input)
fileFirst := strings.Replace(r, encodedExt, "00"+encodedExt, 1)
fileGroup := strings.Replace(r, encodedExt, "%02d"+encodedExt, 1)
return fileFirst, fileGroup
}

//==========================================================================
// Support functions

Expand Down

0 comments on commit f8b25e1

Please sign in to comment.