Skip to content

Instantly share code, notes, and snippets.

@sivinnguyen
Created May 27, 2021 07:30
Show Gist options
  • Save sivinnguyen/8bfba5b44aaf5d078030a2f1a229f9bd to your computer and use it in GitHub Desktop.
Save sivinnguyen/8bfba5b44aaf5d078030a2f1a229f9bd to your computer and use it in GitHub Desktop.
Videos mearging for youtube uploading with chapters

Merge videos for youtube uploading

Calculate duration of videos then export to file

for mp4 in *.mp4
do
    l=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$mp4")
    echo $l ${mp4%.*}
done | awk '
        NR == 1{
                prev = $1;
                $1 = "00:00";
                print;
        }
        NR > 1{
                sum += prev;
                prev = $1;

                h = int(sum / 3600);
        m = int((sum - (h*3600)) / 60);
            s = int(sprintf("%.0f", sum - (h*3600) - (m*60)));

                if(s < 10)
                        s = sprintf("0%s", s);

                if(m < 10)
                        m = sprintf("0%s", m);

                if(h > 0)
                        $1 = sprintf("%s:%s:%s", h, m, s);
                else
                        $1 = sprintf("%s:%s", m, s);

                print;
        }
        BEGIN{
                printf("Chapters:\n")
                printf("-----------------------------------------------------------------------------------\n");
        }

Merging

ffmpeg -f concat -safe 0 -i <(printf "file '$PWD/%s'\n" ./*.mp4) -c copy output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment