User Tools

Site Tools


howto:merge-video

This is an old revision of the document!


Merge/Combine/Join Video

MPG/MPEG files:

sudo apt-get install mpgtx
mpgjoin vid1.mpg vid2.mpg vid3.mpg -o combinedvideo.mpg

WMV/MP4 files (same codec/properties):

ls -la > list.txt
#edit list and sort/format lines as
# file 'video1.wmv'
# file 'video2.wmv'
# file 'video3.wmv'

#or

ls -1 *.wmv | sort -n | sed "s/\(.*\)/file '\1'/" > list.txt

ffmpeg -f concat -safe 0 -i list.txt -c copy outputfilename.wmv

#or

ffmpeg -f concat -safe 0 -i video1.wmv video2.wmv video3.wmv -c copy outputfilename.wmv

https://stackoverflow.com/questions/49371422/how-to-merge-two-videos-without-re-encoding/49373401#49373401

Script for multiple conversions based on filename pattern. Converts all related files that have a number at the end and start with 1:

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in *1.wmv
do
  shortfile=${f::-5}
  echo "file '"$shortfile"1.wmv" > list.txt
  echo "file '"$shortfile"2.wmv" >> list.txt
  echo "file '"$shortfile"3.wmv" >> list.txt
  ffmpeg -f concat -safe 0 -i list.txt -c copy $shortfile.wmv
  rm list.txt
done
IFS=$SAVEIFS

Script to merge files:

video_merge.sh
#!/bin/bash
 
# Check if a parameter is provided
if [ -z "$1" ]; then
    echo "Usage: "$(basename $0)" <files>"
    exit 1
fi
 
TMPFILE=$(mktemp)
 
for input in "$@";
do
    if [ -f "$input" ]; then
        if [ "$container" == "" ]; then
            container="${input##*.}"
        fi
        if [ "$container" != "${input##*.}" ]; then
            echo "Files need to be in same format to merge!"
            echo "Abandoning merge"
            if [ -f "$TMPFILE" ]; then
                rm $TMPFILE
            fi
            echo;read -rsn1 -p "Press any key to continue . . .";echo
            exit
        fi
        echo "file '$input'" >> $TMPFILE
        container="${input##*.}"
        filename=${input%.*}
    fi
done
 
output="$filename.mergedprogress.$container"
sort -n -o $TMPFILE"_sort" $TMPFILE
 
echo "The following files will be merged in this order:"
cat $TMPFILE"_sort"
echo;read -rsn1 -p "Press any key to continue . . .";echo
 
cmd='ffmpeg -f concat -safe 0 -i "'"$TMPFILE"'_sort" -c copy "'"$output"'"'
eval $cmd
if [ $? -eq 0 ];
then
    actualheight=$(ffprobe -v error -select_streams v -show_entries stream=height -of csv=p=0:s=x "$output")
    #output2="${output%.*}."$actualheight"p.$container"
    output2="${output//.mergedprogress./.$actualheight""p.merged.}"
    mv "$output" "$output2"
    echo "Output file: $output2"
    rm $TMPFILE
    rm $TMPFILE"_sort"
else
    echo "An error occured. File not converted properly!"
    echo "Full ffmpeg command:"
    echo "$cmd"
fi
 
echo;read -rsn1 -p "Press any key to continue . . .";echo

Nemo action to call this merge script (to be put in ~/.local/share/nemo/actions:

video_merge.nemo_action
[Nemo Action]
Name=Video Merge
Comment=Video Merge
Exec=video_merge.sh %F
Icon-Name=stock_down
Selection=m
Extensions=mp4;wmv;avi;mkv;mov
Quote=double
EscapeSpaces=true
Terminal=true
howto/merge-video.1709006088.txt.gz · Last modified: 2024/02/27 03:54 by Wulf Rajek