howto:merge-video
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| howto:merge-video [2022/09/04 12:09] – Wuff | howto:merge-video [2025/12/07 03:54] (current) – Wuff | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Merge/ | ====== Merge/ | ||
| + | |||
| + | GUI tool for mkv files. Older version is in default debian/ | ||
| + | < | ||
| + | sudo wget -O / | ||
| + | |||
| + | cat <<EOF | sudo tee / | ||
| + | deb [arch=amd64 signed-by=/ | ||
| + | deb-src [arch=amd64 signed-by=/ | ||
| + | EOF | ||
| + | sudo apt update | ||
| + | sudo apt install mkvtoolnix mkvtoolnix-gui | ||
| + | </ | ||
| + | |||
| + | {{.: | ||
| MPG/MPEG files: | MPG/MPEG files: | ||
| Line 7: | Line 21: | ||
| </ | </ | ||
| - | WMV files (same codec/ | + | WMV/MP4 files (same codec/ |
| < | < | ||
| ls -la > list.txt | ls -la > list.txt | ||
| Line 20: | Line 34: | ||
| ffmpeg -f concat -safe 0 -i list.txt -c copy outputfilename.wmv | 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:// | https:// | ||
| Line 35: | Line 54: | ||
| echo "file '" | echo "file '" | ||
| ffmpeg -f concat -safe 0 -i list.txt -c copy $shortfile.wmv | ffmpeg -f concat -safe 0 -i list.txt -c copy $shortfile.wmv | ||
| + | rm list.txt | ||
| done | done | ||
| IFS=$SAVEIFS | IFS=$SAVEIFS | ||
| </ | </ | ||
| + | |||
| + | Script to merge files: | ||
| + | <code bash video_merge.sh> | ||
| + | #!/bin/bash | ||
| + | |||
| + | # Check if a parameter is provided | ||
| + | if [ -z " | ||
| + | echo " | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | TMPFILE=$(mktemp) | ||
| + | |||
| + | for input in " | ||
| + | do | ||
| + | if [ -f " | ||
| + | if [ " | ||
| + | container=" | ||
| + | fi | ||
| + | if [ " | ||
| + | echo "Files need to be in same format to merge!" | ||
| + | echo " | ||
| + | if [ -f " | ||
| + | rm $TMPFILE | ||
| + | fi | ||
| + | echo;read -rsn1 -p "Press any key to continue . . ."; | ||
| + | exit | ||
| + | fi | ||
| + | echo "file ' | ||
| + | container=" | ||
| + | filename=${input%.*} | ||
| + | fi | ||
| + | done | ||
| + | |||
| + | output=" | ||
| + | sort -V -o $TMPFILE" | ||
| + | |||
| + | echo "The following files will be merged in this order:" | ||
| + | cat $TMPFILE" | ||
| + | echo;read -rsn1 -p "Press any key to continue . . ."; | ||
| + | |||
| + | cmd=' | ||
| + | eval $cmd | ||
| + | if [ $? -eq 0 ]; | ||
| + | then | ||
| + | actualheight=$(ffprobe -v error -select_streams v -show_entries stream=height -of csv=p=0:s=x " | ||
| + | # | ||
| + | output2=" | ||
| + | mv " | ||
| + | echo " | ||
| + | rm $TMPFILE | ||
| + | rm $TMPFILE" | ||
| + | else | ||
| + | echo "An error occured. File not converted properly!" | ||
| + | echo "Full ffmpeg command:" | ||
| + | echo " | ||
| + | fi | ||
| + | |||
| + | echo;read -rsn1 -p "Press any key to continue . . ."; | ||
| + | </ | ||
| + | |||
| + | Nemo action to call this merge script (to be put in ~/ | ||
| + | <code - 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; | ||
| + | Quote=double | ||
| + | EscapeSpaces=true | ||
| + | Terminal=true | ||
| + | </ | ||
| + | |||
| + | alternative merge script with chapters, inspired by https:// | ||
| + | <code bash video_mergechapters.sh> | ||
| + | #!/bin/bash | ||
| + | # | ||
| + | set -euo pipefail | ||
| + | IFS=$' | ||
| + | |||
| + | cd " | ||
| + | echo " | ||
| + | ## Script to merge all mp4 videos in current directory (recursively 2 levels) | ||
| + | ## And update chapter marks to retain the folder/ | ||
| + | |||
| + | ## Script for merging videos | ||
| + | |||
| + | temp_dir=$(mktemp -d) | ||
| + | function finish { | ||
| + | rc=$? | ||
| + | if [[ $rc != 0 ]]; then | ||
| + | echo | ||
| + | echo " | ||
| + | fi | ||
| + | echo -n " | ||
| + | rm -rf " | ||
| + | echo " | ||
| + | exit $rc | ||
| + | } | ||
| + | trap finish SIGHUP EXIT | ||
| + | |||
| + | current_dir=$(pwd) | ||
| + | bname=$(basename ${current_dir}) | ||
| + | final_mp4=${bname}.mp4 | ||
| + | input_list=${temp_dir}/ | ||
| + | file_list=${temp_dir}/ | ||
| + | meta_file=${temp_dir}/ | ||
| + | |||
| + | #Hopefully this will work for either BSD or GNU sed | ||
| + | extended_match=" | ||
| + | echo "" | ||
| + | |||
| + | if [ -e " | ||
| + | echo " | ||
| + | echo;read -rsn1 -p "Press any key to continue . . ."; | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | echo -n " | ||
| + | find . -maxdepth 2 -type f -iname ' | ||
| + | sed -e ' | ||
| + | tee " | ||
| + | awk " | ||
| + | echo " | ||
| + | |||
| + | cat " | ||
| + | echo;read -rsn1 -p "Press any key to continue . . ."; | ||
| + | |||
| + | ## chapter marks | ||
| + | #Do this first so we fail early | ||
| + | #TODO: Test (‘=’, ‘;’, ‘#’, ‘\’) are escaped | ||
| + | ts=0 | ||
| + | echo -n " | ||
| + | ffmpeg -i " | ||
| + | |||
| + | cat " | ||
| + | do | ||
| + | ds=$(ffprobe -v quiet -of csv=p=0 -show_entries format=duration " | ||
| + | # echo " | ||
| + | escaped_title=$(echo ${file} | sed ${extended_match} -e ' | ||
| + | echo " | ||
| + | echo " | ||
| + | echo " | ||
| + | ts=$(awk "BEGIN {print ${ts}+${ds}; | ||
| + | echo " | ||
| + | echo " | ||
| + | done | ||
| + | echo " | ||
| + | |||
| + | echo -n " | ||
| + | ffmpeg -f concat -safe 0 -i " | ||
| + | echo " | ||
| + | |||
| + | echo "Job Completed." | ||
| + | |||
| + | echo;read -rsn1 -p "Press any key to continue . . ."; | ||
| + | </ | ||
| + | |||
| + | Notes re subtitles: | ||
| + | note: mp4 format does not support ass or srt subtitles, but mov_text subtitles. ffmpeg can convert them. formatting from ass subtitles will be lost. | ||
| + | |||
| + | Embed srt subtitle file into mp4 as additional user-controllable track: | ||
| + | < | ||
| + | |||
| + | set metadata of first subtitle stream to english: | ||
| + | < | ||
| + | ffmpeg -i infile.mp4 -i infile.ass -c copy -c:s mov_text -metadata: | ||
| + | </ | ||
| + | |||
| + | extract subtitle from video: | ||
| + | < | ||
| + | |||
| + | Multiple subs require mapping, otherwise first sub will be overwritten | ||
| + | First add another input: -i input2.srt. Second, map that as 2nd stream: -map 2:0. Finally, select encoder for 2nd subtitle stream (the same as the first one): -c:s srt. The complete example\ < | ||
| + | |||
| + | convert srt to ass or ass to srt: | ||
| + | < | ||
| + | ffmpeg -i input.ass input.srt | ||
| + | </ | ||
| + | |||
| + | Remove audio from a video file: | ||
| + | < | ||
| + | ffmpeg -i input_file -c copy -an output_file | ||
| + | |||
| + | # remove all but a specific language audio streams | ||
| + | ffmpeg -i input_file -map 0 -map -0:a -map 0: | ||
| + | # loop to remove all but turkish audio streams: | ||
| + | for f in *.mkv; do ffmpeg -hide_banner -i " | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | |||
howto/merge-video.1662289792.txt.gz · Last modified: (external edit)