From 666f8d496ca3d8269ba0a21d032ddbaec9de5fb8 Mon Sep 17 00:00:00 2001 From: Jan Jastrow Date: Mon, 6 Mar 2023 21:09:36 +0100 Subject: [PATCH] =?UTF-8?q?ChatGPT=20rewrite=20=F0=9F=99=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zsh/ffmpeg | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/zsh/ffmpeg b/zsh/ffmpeg index c7c800f..d9f1277 100644 --- a/zsh/ffmpeg +++ b/zsh/ffmpeg @@ -2,22 +2,39 @@ # filename: /zsh/ffmpeg #-------------------------------- -# ffmpeg-recontainer -# source: https://yohanes.gultom.me/2016/05/21/bash-script-to-batch-convert-mkv-to-mp4-linux/ -function ff-mp4() { - findpath=$1 - : "${findpath:="."}" - find "$findpath" \( -iname '*.mkv' -o -iname '*.flv' -o -iname '*.mov' -o -iname '*.mp4*' \) | while read -r f ; do - dir=$(dirname "$f"); - file=$(basename "$f"); - name="${file%.*}"; - ext="${file##*.}"; +# Original script: https://yohanes.gultom.me/2016/05/21/bash-script-to-batch-convert-mkv-to-mp4-linux/ +# Rewritten with ChatGPT 🙈 - ffmpeg -hide_banner -loglevel error -i "$f" -map 0 -c copy -pix_fmt yuv420p -movflags faststart "$dir/temp_$name.mp4"; +function ff-mp4 () { + local findpath="${1:-.}" + local file_list=() + local temp_file=$(mktemp) - mv "$f" "$dir/_$file"; - mv "$dir/temp_$name.mp4" "$dir/$file"; - done + # Find all video files and write them to a temporary file + find "$findpath" \( -iname '*.mkv' -o -iname '*.flv' -o -iname '*.mov' -o -iname '*.mp4' \) -print0 > "$temp_file" + + # Read the file contents into the file_list array + while IFS= read -r -d $'\0' file; do + file_list+=("$file") + done < "$temp_file" + rm "$temp_file" # Delete the temporary file + + # Process each file in the file_list array + for file in "${file_list[@]}"; do + local dir=$(dirname "$file") + local file_name=$(basename "$file") + local name="${file_name%.*}" + local ext="${file_name##*.}" + + echo "Processing file: $file_name" + + # Re-container the video file to mp4 using ffmpeg + ffmpeg -hide_banner -loglevel error -i "$file" -map 0 -c copy -pix_fmt yuv420p -movflags faststart "$dir/temp_$name.mp4" + + # Rename the original file and move the new mp4 file to its place + mv -n "$file" "$dir/_$file_name" + mv -n "$dir/temp_$name.mp4" "$dir/$file_name" + done } # ffmpeg-convert