I'm trying to batch convert different video files into mkv using arrays in a bash script using mkvmerge or ffmpeg - TagMerge
2I'm trying to batch convert different video files into mkv using arrays in a bash script using mkvmerge or ffmpegI'm trying to batch convert different video files into mkv using arrays in a bash script using mkvmerge or ffmpeg

I'm trying to batch convert different video files into mkv using arrays in a bash script using mkvmerge or ffmpeg

Asked 1 years ago
1
2 answers

In the menu_option_two() function, the statement

for f in *.${auto_ext[*]}

is expanded as:

for f in *.avi mp4 m4p m4v webm ...

assiging f to each of globbed files of *.avi (if exist), mp4, m4p, ...

You will instead need to create double loop as:

for ext in "${auto_ext[@]}"; do
    for f in *."$ext"; do
        if [[ -f $f ]]; then
            mkvmerge -o "${f%$ext}mkv" "$f"
        fi
    done
done

The if [[ -f $f ]] condition is required to avoid the variable f is assigned to literal *.avi, e.g., when the type glob fails. (Alternatively you can set nullglob shopt option.)

Source: link

0

What I ended up doing is, as Ishiano suggested, a double loop, but with a little bit different twist.

for ext in ${auto_ext_2[@]}; do
    count_file=`ls -1 *.${ext} 2>/dev/null | wc -l`
    if [ $count_file != 0 ]
        then 
            for f in *.$ext; do
                ffmpeg -i "$f" "${f%.$ext}.mkv";
            done
    fi

I wanted to check to see if the extension was actually in the directory after running the first part of the double loop to convert the files. The line

count_file=`ls -1 *.${ext} 2>/dev/null | wc -l

checked to see if there was any files of the extension greater than zero within the directory and, if the count_file quantity was greater than zero, it would run the second section of the double loop, if the quantity was zero it would skip the second part altogether. My process was a little different but it worked, so I'm happy.

Source: link

Recent Questions on arrays

    Programming Languages