Skip to content

Commit

Permalink
replace ffprobe with ffmpeg version to get video length
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvire committed May 17, 2022
1 parent 0aa2782 commit f1f8cc6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
51 changes: 39 additions & 12 deletions src/ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,7 @@ func MergeTempVideos(Images []string, Transitions []string, TransitionDurations
settb += fmt.Sprintf("[%d:v]tpad=stop_mode=clone:stop_duration=%f[v%d];", i, transition_duration/2, i)

//get the current video length in seconds
cmd := CmdGetVideoLength(fmt.Sprintf(tempPath+"/temp%d-%d.mp4", i, totalNumImages))

output, err := cmd.CombinedOutput()
CheckCMDError(output, err)

//store the video length in an array
video_each_length[i], err = strconv.ParseFloat(strings.TrimSpace(string(output)), 8)
video_each_length[i] = GetVideoLength(fmt.Sprintf(tempPath+"/temp%d-%d.mp4", i, totalNumImages))

//get the total video length of the videos combined thus far in seconds
video_total_length += video_each_length[i]
Expand Down Expand Up @@ -233,11 +227,7 @@ func MergeTempVideosOldFade(Images []string, TransitionDurations []string, Timin
}

//get the current video length in seconds
cmd := CmdGetVideoLength(fmt.Sprintf(tempLocation+"/temp%d-%d.mp4", i, totalNumImages))
output, err := cmd.CombinedOutput()
CheckCMDError(output, err)

video_each_length[i], err = strconv.ParseFloat(strings.TrimSpace(string(output)), 8)
video_each_length[i] = GetVideoLength(fmt.Sprintf(tempLocation+"/temp%d-%d.mp4", i, totalNumImages))

//get the total video length of the videos combined thus far in seconds
video_total_duration += video_each_length[i]
Expand Down Expand Up @@ -376,3 +366,40 @@ func CreateOverlaidVideoForTesting(finalVideoDirectory string, trueVideo string,
output, err := cmd.CombinedOutput()
CheckCMDError(output, err)
}

func ParseVideoLength(output string) float64 {
re := regexp.MustCompile(`Duration: (?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2}\.\d{2})`)
match := re.FindStringSubmatch(output)
if match == nil {
log.Fatal(match)
}

hour, err := strconv.ParseInt(string(match[1]), 10, 8)
if err != nil {
log.Fatal(err)
}

minute, err := strconv.ParseInt(string(match[2]), 10, 8)
if err != nil {
log.Fatal(err)
}

second, err := strconv.ParseFloat(string(match[3]), 8)
if err != nil {
log.Fatal(err)
}

return second + float64(60*minute) + float64(3600*hour)
}

func GetVideoLength(inputPath string) float64 {
fmt.Println("File: " + inputPath)
cmd := CmdGetVideoLength(inputPath)
output, err := cmd.CombinedOutput()
CheckCMDError(output, err)

outputString := string(output)
fmt.Println("Output: " + outputString)

return ParseVideoLength(outputString)
}
31 changes: 15 additions & 16 deletions src/ffmpeg/ffmpeg_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"log"
"math"
"os/exec"
"strconv"
"strings"
)

/* Function to get the ffmpeg version
Expand Down Expand Up @@ -60,16 +58,21 @@ func CmdTrimLengthOfVideo(duration string, tempPath string) *exec.Cmd {
/* Function to get the length (seconds) of a video
*
* Parameters:
* inputDirectory - the directory of the video to find the length of
* inputPath - the path of the video to find the length of
* Returns:
exectauble command
*/
func CmdGetVideoLength(inputDirectory string) *exec.Cmd {
cmd := exec.Command("ffprobe",
"-v", "error",
"-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1",
inputDirectory,
func CmdGetVideoLength(inputPath string) *exec.Cmd {
// cmd := exec.Command("ffprobe",
// "-v", "error",
// "-show_entries", "format=duration",
// "-of", "default=noprint_wrappers=1:nokey=1",
// inputPath,
// )
cmd := exec.Command("ffmpeg",
"-hide_banner",
"-i", inputPath,
"-f", "null", "-",
)

return cmd
Expand Down Expand Up @@ -175,14 +178,10 @@ func checkSign(num float64) string {
func trimEnd(tempPath string) {
fmt.Println("Trimming end of merged video...")

cmd := CmdGetVideoLength(tempPath + "/video_with_no_audio.mp4")
output, err := cmd.CombinedOutput()
CheckCMDError(output, err)

video_length, err := strconv.ParseFloat(strings.TrimSpace(string(output)), 8)
video_length := GetVideoLength(tempPath + "/video_with_no_audio.mp4")

//match the video length of the merged video with the true length of the video
cmd = CmdTrimLengthOfVideo(fmt.Sprintf("%f", video_length), tempPath)
output, err = cmd.CombinedOutput()
cmd := CmdTrimLengthOfVideo(fmt.Sprintf("%f", video_length), tempPath)
output, err := cmd.CombinedOutput()
CheckCMDError(output, err)
}
4 changes: 4 additions & 0 deletions src/slideshow/slideshow.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func (s slideshow) ScaleImages(lowQuality bool) {
* v - verbose flag to determine what feedback to print
*/
func (s slideshow) CreateVideo(useOldfade bool, tempDirectory string, outputDirectory string, v bool) {
if v {
fmt.Println("Temp Directory: " + tempDirectory)
fmt.Println("Output Directory: " + outputDirectory)
}
// Checking FFmpeg version to use Xfade
fmt.Println("Checking FFmpeg version...")
var fadeType string = FFmpeg.ParseVersion()
Expand Down

0 comments on commit f1f8cc6

Please sign in to comment.