So it really annoyed me to have lots of tvshows with ‘Unknown’ audio stream in my library, and I found out, Plex has serious problem with the .avi container. Since some of the really old tvshows only available in SD with XviD codec, I had to figure out a solution. The basic idea was to convert them into a compatible container. My choice was the .mkv. Found a script made by @ntrevena - thx a lot for them - and modified it a bit, since I didn’t want to reencode those files, only wanted to remux them, change the encoder, and name the audio streams. Had to tweak it a bit, because it handled filenames containing “.” pretty bad - Family.Guy.S06E01.HUN.ENG-m493.avi → Family.mkv -, but now it works like a charm.
Looks for .avi or .mp4 files
It works recursively in all subfolders
Deletes the original .avi or .mp4 file
Names the primary audio stream to English
Deletes the .sh file to clean out the mess
Save the quote into a .sh file (debian/ubuntu script)
Run the following commands:
#!/bin/bash
# A simple script to convert all files in a given folder to a playable MKV format
####################################################
# Workarounds for known issues
####################################################
#
# If it fails with a "No Interpreter" error, try running: sed -i -e 's/\r$//' convert2mkv.sh
#
####################################################
# Edit these variables as required (do not work yet)
####################################################
container="mkv" # Choose output container (mkv; mp4 only)
tempdir="/tmp" # Temporary directory for transcodes (required)
####################################################
# You can set filetypes to parse here (remember to not use the same types as your container above)
####################################################
filetypes=("**/*.mp4" "**/*.avi")
####################################################
# Don't change anything beyond this point!
####################################################
# Disable case sensativity
shopt -s nocaseglob
shopt -s globstar
# Search file type
for i in ${filetypes[*]}; do
path=$(readlink -m "$i")
filename="${path##*/}"
dirpath=${path%/*}
echo "Currently Testing File: " "$filename"
# Run transcode
ffmpeg -fflags +genpts -i "$i" -map 0 -c: copy -metadata:s:a:0 language=eng $tempdir/"${filename%.*}".$container && \
mv $tempdir/"${filename%.*}".$container "$dirpath"/"${filename%.*}".$container
rm -f "$dirpath""/""$filename"
rm -f "remux.sh"
echo "Completed"
done
shopt -u nocaseglob
This format already supports tagging of languages.
This will destroy any subtitles a MP4 file might have when muxing to MKV.
I would use -map 0 -c: copy to copy all tracks.
ffmpeg can also set the lang of tracks at the time of muxing/remuxing. -metadata:s:a:0 language=eng
This will set the first audio track(0-based index) to english.
It also reads AVI
Here is my primitive batch file for Windows, which is probably very easy to convert to bash script
for %%f in (*.avi) do (
echo %%~nf
"C:\Program Files\MKVToolNix\mkvmerge" -o "%%~nf.mkv" --language 0:ger --language 1:jpn "%%~nf.avi"
)
It tags the video stream 0 with a language as well (I use this for anime to indicate the language of “hard” subs. This won’t appear in Plex though, except in the mediainfo.)
thx man! I’ve updated the script. now it’s more efficient.
Correction. I’ve been testing it around, and it still has a problem with mp4, embedded subtitle.
[matroska @ 0x55c286bfd800] Subtitle codec 94213 is not supported.
av_interleaved_write_frame(): Function not implemented
Last message repeated 1 times
Error writing trailer of /tmp/XXXXX.XXXXX.S04E11.WEBRip.x264-TURBO.mkv: Function not implemented
okay, I’ve decided to let go of remuxing mp4 files.
the basic idea was I can’t name language tracks on mp4 files.
at least I had no luck with mkvproedit. but you guys made it clear, I don’t need mkvproedit. will try to split this script to two different parts.
won’t work with AVI files, but that’s okay, since I would remux them anyway.
so I need branches. first to get the extension. if it’s avi, the existing script should run.
if it’s an mp4, mediainfo should check if language is unknown.
if it is, I can make the language rename branch.
if it is eng already, it should skip that mp4 file.
Could anyone help me with this? I’m not an expert of coding
Note the "%~dpn1.mp4.mp4" at the end. The extra “.mp4” is because ffmpeg will not edit existing files in-place.
Though, you could always save to a different folder like say "%~dp1CONVERTED\%~n1.mp4".
That way you can keep it’s original name.