Skip to content

Instantly share code, notes, and snippets.

@palaniraja
Last active May 7, 2023 17:49

Revisions

  1. palaniraja revised this gist Jun 19, 2018. 1 changed file with 18 additions and 8 deletions.
    26 changes: 18 additions & 8 deletions merge-mp4.sh
    Original file line number Diff line number Diff line change
    @@ -5,27 +5,30 @@

    ## Script for merging videos


    filename=`basename pwd`
    current=`pwd`
    bname=`basename "$current"`
    find . -maxdepth 2 -iname '*.mp4' | xargs -L 1 echo | awk '{printf "file \x27%s\x27\n", $0}' >> list.txt
    find . -maxdepth 2 -iname '*.mp4' | xargs -L 1 echo | awk '{print $0}' >> files.txt
    ffmpeg -f concat -safe 0 -i list.txt -c copy "$bname.mp4" -v warning

    echo -n "Merging the files"
    ffmpeg -f concat -safe 0 -i list.txt -c copy "$bname.mp4" -v quiet
    echo "..........[ DONE ]"

    ## extract meta
    # ffmpeg -i all.mp4 -f ffmetadata metafile
    metafile="metadata.txt"

    ffmpeg -i "$bname.mp4" -f ffmetadata $metafile -v warning

    echo -n "Extracting meta data"
    ffmpeg -i "$bname.mp4" -f ffmetadata $metafile -v quiet
    echo "..........[ DONE ]"

    ## chapter marks
    #TODO: (‘=’, ‘;’, ‘#’, ‘\’) to be escaped
    ts=0
    echo -n "Identifying chapters"
    cat files.txt | while read file
    do
    ds=`ffprobe -v warning -of csv=p=0 -show_entries format=duration "$file"`
    ds=`ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$file"`
    # echo "$ds"
    echo "[CHAPTER]" >> $metafile
    echo "TIMEBASE=1/1" >> $metafile
    @@ -35,12 +38,19 @@ do
    echo "TITLE=$file" >> $metafile

    done

    echo "..........[ DONE ]"
    ## update meta with chaptermarks

    ffmpeg -i "$bname.mp4" -i $metafile -map_metadata 1 -codec copy "$bname-meta.mp4" -v warning
    echo -n "Adding chapter meta "

    ffmpeg -i "$bname.mp4" -i $metafile -map_metadata 1 -codec copy "$bname-meta.mp4" -v quiet
    echo "..........[ DONE ]"

    ## cleanup
    echo -n "Cleaning up"

    rm files.txt list.txt $metafile

    echo "..........[ DONE ]"

    echo "Job Completed."
  2. palaniraja created this gist Jun 19, 2018.
    46 changes: 46 additions & 0 deletions merge-mp4.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #!/bin/bash

    ## Script to merge all mp4 videos in current directory (recursively 2 levels)
    ## And update chapter marks to retain the folder/filename

    ## Script for merging videos

    filename=`basename pwd`
    current=`pwd`
    bname=`basename "$current"`
    find . -maxdepth 2 -iname '*.mp4' | xargs -L 1 echo | awk '{printf "file \x27%s\x27\n", $0}' >> list.txt
    find . -maxdepth 2 -iname '*.mp4' | xargs -L 1 echo | awk '{print $0}' >> files.txt
    ffmpeg -f concat -safe 0 -i list.txt -c copy "$bname.mp4" -v warning


    ## extract meta
    # ffmpeg -i all.mp4 -f ffmetadata metafile
    metafile="metadata.txt"

    ffmpeg -i "$bname.mp4" -f ffmetadata $metafile -v warning


    ## chapter marks
    #TODO: (‘=’, ‘;’, ‘#’, ‘\’) to be escaped
    ts=0
    cat files.txt | while read file
    do
    ds=`ffprobe -v warning -of csv=p=0 -show_entries format=duration "$file"`
    # echo "$ds"
    echo "[CHAPTER]" >> $metafile
    echo "TIMEBASE=1/1" >> $metafile
    echo "START=$ts" >> $metafile
    ts=`echo $ts + $ds | bc`
    echo "END=$ts" >> $metafile
    echo "TITLE=$file" >> $metafile

    done

    ## update meta with chaptermarks

    ffmpeg -i "$bname.mp4" -i $metafile -map_metadata 1 -codec copy "$bname-meta.mp4" -v warning


    ## cleanup
    rm files.txt list.txt $metafile