How do I use ffmpeg / mkvtoolnix to strip non-English audio streams and subtitle streams?

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
Pmkswiii
Posts: 3
Joined: 12 Oct 2023, 12:47

How do I use ffmpeg / mkvtoolnix to strip non-English audio streams and subtitle streams?

Post by Pmkswiii »

After trying a few different found online solutions that most were python scripts or making a bat file use ffmpeg and mkvtoolnix (the gui front end for command line backend tools) I read a few comments and found this on another forum. I don’t remember the specific one so I’m not able to give specific credit. I can say it’s not mine. Also it seems to work in a limited way. It’ll do what is asked but instead of all files in a folder it will process some. Not sure why not all. Anyway the goal is basically the same but done in filebot using the groovy scripting. I basically don’t want any subtitles, I don’t want other language selections. Less to get set right only to have it start again the next episode etc.
At present the goal is to:
—————
Filebot correctly renames the file via AMC.
- demux the file, so audio streams and video streams can be selected specifically.

- Remux with only selected audio stream (English), set English Audio track as the first and only audio stream. Copy the video stream without modifying it. Remux to .MKv file and output to different folder.

-Retitle remux to match the file name and save.
————————-
##The code found to do this##

batch: Select all

@echo off
if not exist NEW\*.* md NEW
if exist temp\*.* (del /q temp\*.*) else md temp

for %%a in ("*.mkv") do call :Process "%%a"
goto :end

:Process
:: Set the first stream, normally the video, to English. (In case it's not English.)
ffmpeg -i "%~1" -vcodec copy -acodec copy -metadata:s:0 language=eng -y "temp\%~1"
:: Copy the English streams (video+audio) to a new video.
ffmpeg -i "temp\%~1" -vcodec copy -acodec copy -map 0:m:language:eng -y "NEW\%~1"
goto :eof

:end
rd /s /q temp
User avatar
rednoah
The Source
Posts: 23002
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Groovy scripting question

Post by rednoah »

:?: Are you using the GUI? :arrow: Custom Post-Processing Scripts › Run Command

:?: Are you using the CLI? :arrow: -exec custom post-process commands



EDIT:

:?: Do you already have prototyped and tested ffmpeg and / or mkvtoolnix commands? You have pasted some ffmpeg commands above but have you tested those commands? Do they do what you want to do?

:idea: Note that filebot itself cannot mux / demux video file containers. You can however script ffmpeg / mkvtoolnix calls as needed. You will need to be very familiar with these tools already though to script them.
:idea: Please read the FAQ and How to Request Help.
Pmkswiii
Posts: 3
Joined: 12 Oct 2023, 12:47

Re: How do I use ffmpeg / mkvtoolnix to strip non-English audio streams and subtitle streams?

Post by Pmkswiii »

Thanks for the quick reply. I have tested those on a windows machine with ffmpeg and mkvtoolnix installed. I will run them on files located offsite but mounted to a local drive using rclone. I’m hoping to set this as part of the file processing workflow so that it can be run as a scheduled task.
Yes the pasted command I have as a .bat file that I keep in my tv and movie folders. When I open a command prompt as admin, and navigate to the file location from command prompt, then I drag and drop that bat file and it works. Quality of results is good but for some reason it only processes 20-ish files/out of 100. I assume that this may be in part due to it it being a lower priority for the system to complete. Or it runs fast for a while but if I open multiple instances it slows. So it works great but never gets the entire folder completely processed. Or maybe there’s only some files with further audio and subs?
My understanding is that mkvtoolnix requires the installation of ffmpeg since it’s used for some of its toolset. Is there a way to call the pasted script at the end of the AMC or something to tidy up the file after processing?
User avatar
rednoah
The Source
Posts: 23002
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Groovy scripting question

Post by rednoah »

rednoah wrote: 12 Oct 2023, 13:46 :?: Are you using the CLI? :arrow: -exec custom post-process commands
If you are using the amc script then adding something like this might work:

Shell: Select all

--def exec="ffmpeg -i {quote(f)} -map 0:v:0 -map 0:a:0 -c copy -y {quote(folder / fn + '.REMUX.' + ext)}"
** untested; should just take the first video stream / first audio stream and remux it into a new file; you will need to do some trial & error & checking what the console output says and iterate step by step
** mkvtoolnix is not used


:idea: Selecting streams with the -map option
:idea: Please read the FAQ and How to Request Help.
Post Reply