I'm working on a script that will search through a directory for MKV files and remux them to MP4. I found a few other scripts around the net that do this, but they all have limitations that don't always get the job done. The main limitation I've found is that most scripts look for the video track in either the first track or the last track, which isn't always true. So I wanted a script that would work with any MKV file, no matter what track the video is on, and also copy all the audio/subtitle tracks, not just one audio and one video.
Whenever I run the script I get the following error for every file it tries to extract the tracks from using mkvextract:
Quote:
Error: Invalid track ID/file name specification in argument '4.mkv"'.
Whenever I type in the mkvextract command and run it myself it works perfect, so I'm not sure why the script has problems executing it.
It's a long script, and could probably be written better, but this is my first attempt at a bash script. I've only ever worked with Web Development languages in the past, so it hasn't been too difficult for me to get this far, but now I'm stuck...
Code:
#!/bin/bash
find . -type f | grep .mkv$ | while read file
do
echo $file
## Gather some information about the file
directory=`dirname "$file"`
title=`basename "$file" .mkv`
tracks=`mkvinfo "$file" | grep "Track type" | sed 's/.*: //'`
maxarray=`mkvinfo "$file" | grep "Track number" | sed 's/.*: //' | tail -n 1`
## Put each of the tracks from the file into an array
n=1
for i in $tracks; do track[$n]=$i ; let n=n+1 ; done
## Loop through each track to figure out what codec it is
## Then store it in an array with the same location as the track
codecs=`mkvinfo "$file" | grep "Codec ID" | sed 's/.*: //'`
n=1
for i in $codecs; do
if [ "$i" == "V_MPEG4/ISO/AVC" ]; then
type[$n]='264'
elif [ "$i" == "A_DTS" ]; then
type[$n]='dts'
elif [ "$i" == "A_AC3" ]; then
type[$n]='ac3'
elif [ "$i" == "A_AAC" ]; then
type[$n]='aac'
elif [ "$i" == "S_TEXT/UTF8" ]; then
type[$n]='srt'
else
echo "Track type unknown."
fi
let n=n+1;
done;
## Loop through the file types and create an array with the file name and track type
n=1
for i in "${type[@]}"; do trcktitle[$n]="${title}.$i"; let n=n+1; done
## Loop through the track file names and search for names that are the same
## If they are the same, then change the file name with an incremental number
## This is done so that when extracting the tracks, each track has it's own file name
## To prevent overwriting tracks of the same type that were already extracted
n=1
for i in "${trcktitle[@]}"; do
q=0
z=1
for p in "${trcktitle[@]}"; do
if [ "$i" == "$p" ]; then
if [ -n "$sametitle" ]; then
sametitle=("${sametitle[@]}" "$z")
else
sametitle=("$n" "$z");
fi
let q=q+1
fi
let z=z+1
done
if [ $q -gt 1 ]; then
t=0
for i in "${sametitle[@]}"; do
trcktitle[$i]="${title}${t}.${type[$i]}"
let t=t+1
done
fi
q=0
let n=n+1
unset sametitle
done
## Let's figure out the FPS of the video track
## Some MKV files don't always put the video track in the same location, so we gather all the FPS information in the file
## Then we figure out which FPS belongs to the video track
## This is done by looping through both the FPS array and the type array
## Assigning the same location in each array based on file type
## If the type is SRT, move on in the type array but stay at the same location in the FPS array
## If AC3, AAC, or DTS, move in in both arrays; if 264 (the video track) assign FPS from array to vfps and leave the loop
trckfps=`mkvinfo "$file" | grep duration | sed 's/.*(//' | sed 's/f.*//'`
n=1
for i in ${trckfps[@]}; do fps[$n]=$i; let n=n+1; done
if [ ${#type[@]} != ${#fps[@]} ]; then
n=1
z=1
for i in ${type[@]}; do
if [ "$i" == "srt" ]; then
let n=n+1
elif [ "$i" == "264" ]; then
vfps=${fps[$z]}
break
else
let z=z+1
let n=n+1
fi
done
else
n=1
for i in ${type[@]}; do
if [ "$i" == "264" ]; then
vfps=${fps[$n]}
break
fi
let n=n+1
done
fi
## Create the options for the MKVEXTRACT command. This will put the track titles in the correct location with track position and type
n=1
while [ $n -le $maxarray ]; do
if [ -n "$extract" ]; then
extract=" ${extract} ${n}:\"${trcktitle[$n]%.*}\".${type[$n]}"
else
extract="mkvextract tracks \"${file}\" ${n}:\"${trcktitle[$n]%.*}\".${type[$n]}"
fi
let n=n+1
done
echo $extract
echo `$extract`
## If the type that was extracted is AC3 or DTS, we need to convert it to AAC before putting everything back together in the MP4 container
# n=1
# for i in ${type[@]}; do
# if ([ "$i" == "ac3" ] || [ "$i" == "dts" ]); then
# old=${trcktitle[$n]}
# trcktitle[$n]=$old.aac
# ffmpeg -i "$old" -acodec libfaac -ab 576k "${trcktitle[$n]}"
# fi
# let n=n+1
# done
#echo $old
#echo ${trcktitle[@]}
## Create all the options that go into the MP4Box command
## Loop through each track title and add it to the input array
# n=1
# while [ $n -le $maxarray ]; do
# if [ -n "$input" ]; then
# input=" $input -add \"${trcktitle[$n]}\""
# else
# input="-add \"${trcktitle[$n]}\""
# fi
# let n=n+1
# done
#echo $input
## Let's put everything back togehter into an MP4 file
# MP4Box -new "${directory}/${title}".mp4 $input -fps $vfps
## Now we need to clean up our mess
## Delete all of the track title files that we extracted from the MKV file
# for i in ${trcktitle[@]}; do
# rm -f "$i"
# done
## If there is an MP4 file created, then let's go ahead and delete the original MKV file
## I would leave this commented so you can test the MP4 file for quality, then delete the original yourself
#if [ -f "${directory}/${title}.mp4 ]; then
#rm -f "$file"
#fi
## Let's clean up some of the variables we used so they dont get reused in the next file
n=""
file=""
directory=""
title=""
tracks=""
maxarray=""
codecs=""
unset type
unset trcktitle
extract=""
input=""
done
It will be hard for anyone to help you with this long script. What I can propose to you is to run it with "bash -x yourscript" to get the shell to print out the commands while it is executing.
You may try to set the PS4 variable to something that is easy for you to debug your script.
Here is a little example. Hope it helps:
Code:
$ export PS4="
--->DEBUG: "
$ cat a.sh
#! /bin/bash
echo Hello World
date
cal
$ bash -x a.sh
--->DEBUG: echo Hello World
Hello World
--->DEBUG: date
Wed May 9 08:08:22 SGT 2012
--->DEBUG: cal
May 2012
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
The script worked perfectly fine, it just wouldn't execute the mkvextract command. However I found a fix... just send the mkvextract command to another file then execute that file. Not ideal, but it works.
I want to run commands inside a bash script.
An example is
I want to pass the command in a string as regexp as an argument to the script, then run sed on the bash variable
sed.sh regexp
sed.sh "-i \"s/<p>//g\""
then call
sed "$regexp" $fl (3 Replies)
Hi All,
Just have a requirement, I am executing a bash shell script, my requirement is to catch the pid and job name to a txt file in the same directory, is there anyway to do it? please help me out.
Regards
Rahul
---------- Post updated at 08:42 AM ---------- Previous update was at... (2 Replies)
Original script written on CentOS 6.3 with GNU bash 4.1.2
Destination system is Solaris 9 with GNU bash 2.05 (not changeable by me)
I have a script written on the linux side but now we need to provide a version to another site that "doesn't like linux". I've been going through changing the ] or... (13 Replies)
code:
IMAGE=$imgvalue;
if
then
echo DO=ABC;
elif
then echo DO=ETC;
elif
then echo DO=XYZ;
else
echo "$imgvalue is unsupported";
exit 1;
fi
in above script IMAGE=1 , IMAGE=2, IMAGE=3 whatever may be the value i have assigned it's showing only DO=ABC other conditions... (7 Replies)
Hello , i am using RHEL5 , i have got a perl script that executes and sends an email correctly if i run it with ./sendemail.sh command. This is the script below
#! /usr/bin/perl
use Net::SMTP;
print "starting email send .";
$smtp = Net::SMTP->new("192.168.0.1");... (2 Replies)
Hi,
I'm building an installation system that uses separate data
files that contain the individual characteristics of each package.
Within the data file, I would like to incorporate a section that
contains a bash script that is loaded into an array which is
then redirected to bash to be... (13 Replies)
Hi all, dummy here.... I have major errors on entering the shell. On login I get:
-bash: dircolors: command not found
-bash: tr: command not found
-bash: fgrep: command not found
-bash: grep: command not found
-bash: grep: command not found
-bash: id: command not found
-bash: [: =: unary... (12 Replies)
Hi all,
I am running a script on bash shell, although it gives me the desired output, it displays some errors at the prompt like
this directory doesnt exists
unary operator
etc etc
Is there someway to avoid these errors ??
Its very annoying to have these errors while the script is... (4 Replies)
Hi,
I tried a simple hello.cc program in Unix.
- Compiled with no errors
- Created a.out successfully
- While executing a.out, following error message appeared
" ld.so.1:a.out fatal: libstdc++.so.2.10.0: Open failed: No such file or directory Killed"
... (1 Reply)