LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2011, 03:26 AM   #1
hysiria
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Rep: Reputation: Disabled
bash script fro removing audio tracks from mkv


hello,

i have a little problem writing a script to remove audio tracks from a mkv file.
what i want to do is:
in a folder are several mkv files with 2 audio tracks.
default track ID is 2 and the other audio track ID is 3.
i want to have a script that automatically removes all audio tracks with ID3 from any mkv file in the folder.
i found a .bat script for windows that is working very well but i dont know how to write that script for linux.

the windows script is as follows:
Code:
FOR /F "delims=*" %%A IN ('dir /b *.MKV') DO "<path_to_mkvmerge>mkvmerge.exe" -o "fixed_%%A" -a !3 --compression -1:none "%%A"
could someone help me please?
 
Old 10-18-2011, 03:47 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi and welcome to LinuxQuestions!

First you need the mkvtoolnix package which provides the mkvmerge command. Then you just need to translate the DOS for loop in a shell version, e.g using bash
Code:
#!/bin/bash
for file in *.mkv
do
  mkvmerge -o "fixed_$file" -a !3 --compression -1:none "$file"
done
This assumes that the extensions of all the mkv files to convert is lowercase and that there are no spaces in the file names. If this is not the case you need some little modification to this script.
 
1 members found this post helpful.
Old 10-18-2011, 03:57 AM   #3
hysiria
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
hey thank you very much for your fast answer.
alot of the mkv's have spaces in their file names but all extensions are lowercase.
how would the script look like to handly spaces in the file names?
 
Old 10-18-2011, 08:46 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Actually, the above loop should have no problem processing filenames with spaces.

The *.mkv globbing pattern expands to a list of all .mkv filenames in the directory, however they're named, and the loop will read each one in turn. Since the shell does the glob expansion step after word-splitting has been completed, each expanded name is treated as a separate argument. Therefore it's safe to use glob input in a for loop.

http://mywiki.wooledge.org/glob

Note that other kinds of input string aren't safe, such as with the output of a command or variable substitution, because parameter expansions happen before word-splitting. The substituted strings will be broken up into individual words before the loop sees them. In these cases, you should use a while+read loop.

http://mywiki.wooledge.org/DontReadLinesWithFor
http://mywiki.wooledge.org/BashFAQ/001

The only other place where spaces would be a problem is when the variable is expanded later on in the mkvmerge command, and for the same reason. But since double-quoting variables protects the expanded string from word-splitting, we're safe here too.

For more on how the shell handles arguments and whitespace, see here:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


Edit: Incidentally, case matching can be handled simply by adding shopt -s nocaseglob to the script, or by using *.[mM][kK][vV] as your glob pattern.

Last edited by David the H.; 10-18-2011 at 09:10 AM. Reason: added a link and expanded info
 
1 members found this post helpful.
Old 10-18-2011, 10:44 AM   #5
hysiria
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
thank you all very much its working like a charm
 
Old 10-21-2011, 03:06 AM   #6
hysiria
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
another question has come to my mind:

is it possible to extend the script in a way that mkvmerge can delete all non german audio tracks from the mkv?
for example i have a mkv with 2 audio tracks (english, german) but the german track (the one i want to keep) is ID3.
i now want mkvmerge to detect that there are an english and german track and then delete the english track regardless of its ID number.

is that even possible?

after that is finished i want to move the folder/file to another directory

my script i have so far:
Code:
cd /media/storage/mkvmerge
for file in *.mkv */*.mkv */*/*.mkv
do
	fdir=$( dirname "$file")
	mkvmerge -o "$fdir/fixed_$file" -a !3 --compression -1:none "$file" 
	mkdir "/media/storage/movies/$fdir"
	mv "$fdir/fixed_$file" "/media/storage/movies/$fdir/"	
	rm -r "$fdir"
done
it basically works but not very well ;(

Last edited by hysiria; 10-21-2011 at 03:13 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
extract audio from a mkv file sylar Linux - Software 16 11-29-2017 02:58 PM
Removing program diretories and files with a bash script wimnat Linux - Software 1 05-25-2009 08:55 PM
removing # from crontab (bash script) inderpunj Programming 4 10-13-2008 02:34 PM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 10:12 AM
.mkv in Xine = No Audio III Linux - Software 0 09-01-2004 08:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration