aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSylvain Herlédan <sylvain.herledan@hrafnagud.info>2026-07-16 22:55:28 +0200
committerSylvain Herlédan <sylvain.herledan@hrafnagud.info>2026-07-16 22:55:28 +0200
commitc7e48fd13a87a5e7bc33a6e6e1086149775c004d (patch)
tree05b8f54d46ecf5670c66726a6677e25d927fbac3
parentbe471469156b5a4fb6d5a0c9c82607a316136393 (diff)
downloadadmin_toolbox-master.tar.gz
admin_toolbox-master.tar.bz2
admin_toolbox-master.zip
Add script for converting AVI file to MKV, while fixing potential audio issues.HEADmaster
-rwxr-xr-xavi2mkv.sh55
1 files changed, 55 insertions, 0 deletions
diff --git a/avi2mkv.sh b/avi2mkv.sh
new file mode 100755
index 0000000..3de6c62
--- /dev/null
+++ b/avi2mkv.sh
@@ -0,0 +1,55 @@
+#!/bin/bash
+
+# See https://video.stackexchange.com/a/32719
+
+set -o nounset
+set -o errexit
+set -o pipefail
+
+readonly ARGS=( "${@}" );
+
+main()
+{
+ local _avi_path;
+ _avi_path="${1}"; shift;
+
+ local _mkv_path;
+ _mkv_path="${1}"; shift;
+
+ local avi_path;
+ avi_path="$(readlink --canonicalize-existing "${_avi_path}")";
+
+ local mkv_path;
+ mkv_path="$(readlink --canonicalize-missing "${_mkv_path}")";
+
+ # Check whether the mp3 header is missing in the input file
+ local missing_count;
+ missing_count=0;
+ set +o errexit
+ missing_count="$(ffprobe -hide_banner -i "${avi_path}" 2>& 1 \
+ | grep -i 'mp3float' \
+ | grep -c -i 'Header missing')";
+ set -o errexit
+
+ if [ 0 -eq "${missing_count}" ];
+ then
+ # Trivial copy
+ ffmpeg -i "${avi_path}" -c:v copy -c:a copy "${mkv_path}"
+ else
+ # Extract the mp3 stream
+ local mp3_stream;
+ mp3_stream="$(mktemp --suffix='.mp3')";
+
+ ffmpeg -i "${avi_path}" -c:a copy "${mp3_stream}"
+
+ # Combine it back into the original AVI file
+ ffmpeg -i "${avi_path}" -i "${mp3_stream}" \
+ -c:v libx264 -c:a copy -map 0:v:0 -map 1:a:0 \
+ "${mkv_path}"
+
+ rm "${mp3_stream}"
+ fi
+}
+
+main "${ARGS[@]}"
+exit 0