Solution for Audio: Unknown

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 :slight_smile:

Save the quote into a .sh file (debian/ubuntu script)
Run the following commands:

sudo chmod +x filename.sh
sudo chmod 777 filename.sh
./filename.sh

enjoy! :slight_smile:

#!/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
1 Like

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.

Additionally, mkvmerge will read mp4 and write mkv natively, no ffmpeg required.

This is the excerpt from my maintenance script

Merge this the above in a find loop and substituting would seem a perfect solution?

  echo  `date` Cleaning: $Episode >> ${Logfile}
  rm -f temp.mkv
  ${Bin}/mkvmerge -o temp.mkv -M -S --no-global-tags --disable-track-statistics-tags "$Episode"
  if [ $? -ne 0 ]; then
    echo `date` Bad file: Error \($?\) "$Episode" >> ${Logfile}
    rm -f temp.mkv
  else
    echo `date` "Keeping: " "$Episode" >> ${Logfile}
   ${Bin}/mkvpropedit -s title=   temp.mkv
   ${Bin}/mkvpropedit --edit track:a1 --set language=eng --edit track:v1 --set language=eng temp.mkv
   mv -f temp.mkv "$Episode"
   rm -f temp.mkv
  fi

2 Likes

No kidding. What a pain!

Actually, besides tidying up. The new PMS update kinda fixes the unknown audio issue. At least with certain aspects of automatic subtitle selection.

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.)

1 Like

@syluccy - Good on you for tweaking the script, and fixing it up for your own purposes. I’m glad to see there are people who still find it useful.

I’ve messaged/replied to you on the original thread. I apologize for not replying sooner. Work has been pretty heavy. :+1:

1 Like

let me try it!
could you edit the full line, you are referring to?

Sorry for late reply. Power supply went on a long vacation…

I’m assuming both of my examples?

This is for Windows. I don’t know the syntax for linux but sure Chuck will.

This will copy ALL(everything possible including all tracks, set languages, track descriptions, chapters, etc)

ffmpeg.exe -i "SourceFile.mp4" -map 0 -c: copy "DestFile.mp4"

Will mux(copy all in the process) and set the video to undetermined and audio track 1 (0-Based Index) to English.

ffmpeg.exe -i "SourceFile.mp4" -map 0 -c: copy -metadata:s:v language=und -metadata:s:a:0 language=eng "DestFile.mp4"

Again, Windows script only.

1 Like

thx man! I’ve updated the script. now it’s more efficient. :wink:

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. :slight_smile:
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.

For mp4 files you might want to look at MP4Box (https://gpac.wp.imt.fr/mp4box/)

I use this one liner which removes metadata (MP4Box seems to do this by default) and sets lang to en.

It reads all mp4 files in directories in your current working directory, processes the files, outputs them to new dir and deletes the originals

find "$PWD/" -name '*.mp4' -print0 | while IFS= read -r -d '' file; do if [ -s "$file" ]; then echo $(basename "$file" ); /path/to/MP4Box -add "$file" -lang 2=en /path/to/output/$(basename "$file" ) -new; rm "$file"; fi done

okay, so I found a solution to get audio language for mp4 files.

mediainfo --Inform="Audio;%Language%" filename.mp4

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 :confused:

I would look at @blim5001 example. While I’m not familiar with the syntax used, the actual command looks like my MP4Box batch file.

for %%f in (*.mp4) do MP4Box.exe -lang 2=eng "%%f"
pause

There are many topics on this subject that might help you.
Here is one.

And you will.
There is no need to convert/mux MP4 files to MKV if all you want to do is set language for audio track(s).

"%~DP0%FFmpeg.exe%" -i "%~1" -map 0 -c: copy -metadata:s:v language=und -metadata:s:a language=eng -metadata:s:s language=eng "%~dpn1.mp4.mp4"

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.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.