MythMediaMonitor is an advanced bash shell script designed to incorporate external media directly into the recordings screen of Mythfrontend. At one time, this required the use of the MythVideo plug-in. MythMediaMonitor supports up to version 0.24.1-fixes of MythTV.
Watch the Features Video for a complete overview of MythMediaMonitor’s functionality.
As of version 0.22, the MythTV internal media player has added support for most known media types. Because of this, mplayer or xine are no longer required to play media. MythMediaMonitor, when run from cron, will monitor folders for TV and movie files, and automatically insert them into the MythTV database. This gives you all the added functionality of the MythTV interface, such as marking shows watched, without the need for navigating into a separate video plug-in.
Features
The following features are currently supported.
- Dynamic selection of Recording Group to insert media into
- Auto extraction of MKV subtitles to SRT for playing in MythTV’s internal player
- Includes language selection option
- DEBUG and RUN flags for testing without modification to your MythTV database
- Grabs the following data from the media file and tvrage.com
- Show title
- Episode number
- Season number
- Subtitle
- Original Air Date
- File size
- Caches tvrage.com data during each run to minimize internet traffic
- Grabs the following data from the media file and IMDB.com
- Movie title
- Plot
- Primary Photo for thumbnail in Mythfrontend
- File size
- TV filename support for the following formats
- */<title>.s##e##.*
- */<title> – ##x##.*
- */<title>_s##e##_*
- */<title> – s##e##.*
- */<title> – s##e##_*
- */<title> – s##e## *
- */<title>.s##e#.*
- */<title>_s##e#_*
- */<title>.s#e##.*
- */<title>_s#e##_*
- */<title>.s#e#.*
- */<title>_s#e#_*
- */<title>.s##.e##.*
- */<title>_s##_e##_*
- */<title>.s##.e#.*
- */<title>_s##_e#_*
- */<title>.s#.e##.*
- */<title>_s#_e##_*
- */<title>.s#.e#.*
- */<title>_s#_e#_*
- */<title>.###.*
- */<title>_###_*
- */<title>.####.*
- */<title>_####_*
- */<title>.##.##.*
- */<title>_##_##_*
- */<title>.##x##.*
- */<title>_##x##_*
- */<title>.#x##.*
- */<title>_#x##_*
- */<title>.##x#.*
- */<title>_##x#_*
- */<title>.#x#.*
- */<title>_#x#_*
- Movie filename support for the following formats
- */<movie title>/filename.ext
- Supports listing of multi-CD rips using the following file name structure
- *cd1*
- *cd2*
- *CD1*
- *CD2*
- Locates the following media types
- iso
- mpg
- mpe
- mpeg
- m1s
- mpa
- mp2
- m2a
- mp2v
- m2v
- m2s
- avi
- mov
- qt
- asf
- asx
- wmv
- wma
- wmx
- real
- rm
- ra
- ram
- rmvb
- mp4
- 3gp
- ogm
- mkv
- flv
- Supports/requires independent TV and movie folders
- Checks for requirements before running
- Deletion of media file when deleted from Mythfrontend (if delete via symlink is enabled)
Upcoming Features
The following features are planned for future versions.
- VIDEO_TS directory support
- Clean up and removal of files once a video is deleted from within MythTV
Requirements
The following are required to run the script, but are included by default in most distributions.
- sed
- tr
- wget
- grep
- mysql
- convert
- mkvinfo*
- mkvextract*
- touch
- sleep
- ln
- du
- awk
- wc
- cat
* The MKV tools are only required if you have the RIPSUBS option enabled.
Usage/Installation
Installation is simple and straightforward. Simply extract mythmediamonitor.sh anywhere on a system with access to your media and recordings directory. Open up the script in vi or your favorite text editor to edit the configuration, and then schedule it to run as often as you like in the /etc/crontab file. A sample crontab entry is below.
* * * * * root /bin/custom/mythmediamonitor.sh
Configuration
The following setup variables should be edited before running MythMediaMonitor for the first time. We strongly recommend using the DEBUG=true and RUN=false settings the first time you run the script to ensure everything will function properly on your system.
Here you will need to enter your mysql credentials
MYSQLUSER="root" MYSQLPASS="" MYSQLDB="mythconverg"
Directory to store data files. Do not use a trailing backslash. This directory must be writeable by the user you run this script with.
DATADIR="/dev/shm/mythmediamonitor"
Directory where your TV files are stored. Do not use a trailing backslash.
TVDIR="/storage/video/mythvideo/TV"
Directory where your movie files are stored. Do not use a trailing backslash.
MOVIEDIR="/storage/video/mythvideo/Movies"
Recordings directory and group as configured in Myth. Do not use a trailing backslash. This will be used to create a symlink to your movie and TV files
RECORDINGDIR="/storage/video/recordings" RECORDINGGROUP="Default"
Select the subtitle language that you would like to strip out of mkv files into srt files for MythTV’s internal player.
RIPSUBS=true SUBLANG="eng"
DEBUG Options
Set RUN to true or false. Enabling this will insert into the database. Use false for testing.
RUN=true
Set DEBUG true to see debug messages printed out
DEBUG=false
Feature Requests/Bug Submissions
Please leave feature requests and bug reports in the comments below. If these grow in number, we will set up a dedicated forum for support.
Download
Please do not distribute MythMediaMonitor outside of this site. Directing links here will ensure the latest version of MythMediaMonitor is always in circulation.
Download the latest version: mythmediamonitor.0.07.tar.gz
Remember to check out the rest of the site!
Change-log
v0.07
- Script now checks to see if it is already running before starting
- Changed default data directory from /tmp to /dev/shm to keep from writing to disk
- Increased delay for web queries to decrease like
v0.06
- Added flv support
v0.05
- Added additional filename parsing support based on user request
v0.04
- Updated to support MythTV 0.24
- Movies are not indexed by default since MythVideo now has increased themoviedb.com functionality (We recommend not including movies in your recordings listing any longer.)
- Buggy IMDB implementation removed
- Handles more TV filename formats
v0.03
- Updated TVRage API usage
- Included new naming conventions for TV show filenames
v0.02
- Fixed bug with quotes in filenames and some strings
- Fixed bug with password supplied in mysql command
- Updated based on tvrage.com change in HTML line breaks
v0.01
- Release
46 Responses to “MythMediaMonitor: Added video support for MythTV”
A couple bugs.
1) The mysql options handling is wrong. You don’t put a space between -u and -p and the user/pass. The block at the end needs to look like this.
MYSQLOPTS=”$MYSQLDB -u$MYSQLUSER”
if [[ -n "$MYSQLPASS" ]]; then
MYSQLOPTS=”$MYSQLOPTS -p$MYSQLPASS”
fi;
The username was also hardcoded to root. I have corrected that in the code above.
2) The directories for movies and TV dirs require a trailing slash for “find’ to find the files. The comments say you don’t need one, and refer to it as a backslash, which is true I suppose, but UNIX systems don’t use a backslash as a directory separator. Examples that work:
# Directory where your TV files are stored. Do not use a trailing
# backslash.
TVDIR=”/data/mythtv/videos/TV/”
# Directory where your movie files are stored. Do not use a
# trailing backslash.
MOVIEDIR=”/data/mythtv/videos/Movies/”
3) SQL queries need to be sanitized. Any filenames with a single quote in them screw up the mysql calls. You should also be checking for SQL injection, just to be safe.
Tested on Mythbuntu 9.10. Can movies be parsed using the filename as well? It would be nice.
Hi Travis,
Thanks for all the quick feedback. I’ve incorporated your edits for #1 and backslash vs. slash into v0.02, which I’ll post shortly. Thanks for pointing out the quotes in filenames, I had them covered in other fields, but missed that in testing.
As for the ‘find’ and slashes, it works without the slash on my Fedora and CentOS test systems. Can you run `find –version` for me and let me know what version is installed in Mythbuntu? Adding an extra slash won’t hurt, it will just cause some commands to have a duplicate slash, which Linux will ignore.
For movie filename parsing, that’s a tough one. I will give it a shot, but there aren’t many filename standardizations, and it will be hard to figure out what is the movie filename vs, what is just extra information on the end of the filename. Once I’ve got your bugs worked out, I’ll look into it for 0.03.
I’m running Myth .22 on Ubuntu 9.10 what’s the recommended way of installing convert, mkvinfo & mkvextract – As I’ve just tried running your script and get the missing dependency for those items.
Thanks
You should be able to do a “yum provides mkvinfo mkvextract convert” to find which packages include those commands. On Fedora, you can use this command: “yum install mkvtoolnix ImageMagick”, which may work for Ubuntu as well.
I’ve installed both via
apt-get install mkvtoolnix imagemagick
Cheers
[...] can download the latest version on our MythMediaMonitor page, or if you are unfamiliar with the product, check out the Features Video in the original [...]
Nice idea, but I’m having a big problem.
1. It matches my movie titles, but not my TV titles. I end up with _NO_TITLE and no metadata.
Doesn’t match on the following TV show patterns, for example:
Angel.1×01.city_of.avi
Angel.s05e15.avi
deadwood.s01.e02.avi
@Donald: Interesting. I’ll take a look at those show formats this weekend. Is it just those, or is it doing it for all your episodes?
It’s all episodes. Not a single one gets through. I’ve got about a dozen shows with 1-7 seasons each. I think those samples are representative of how I named them.
One more thing. I thought it as because I had my TV shows arranged like:
/TV/Title/Season/Episode
I tried removing the Season folder, but they still don’t go through.
Let me know if you have idea that jumps out at you, and I can try to work it from end as well.
@Benjamin: I was having the same problem as Donald – to fix this I needed the remove the double quotes that were around the regular expressions. so instead of =~”blahblah” ]] it’s just =~ blahblah ]]
From tldp.org :
As of version 3.2 of Bash, expression to match no longer quoted.
Hi,
This looks really cool but I have a question before I install it… It looks like it adds the info for each media file into the mythconverge db, correct? If I have issues or just want to uninstall later, Is there a way to undo the changes or do I need to go into the menus and delete each file?
Actually that brings up another question:All my DVDs are ripped to ISO on a fileshare. If I add them using MythMediaMonitor and a user deletes them from the Myth UI, does it delete the actual files or just the entry in Myth?
@Casey: Thanks for the input, I’ll make that update for 0.04, and hopefully release in the next few days.
@Donald: For TV shows, folder structure won’t matter as it is reading the episode and season from the filenames.
@JerkyChew: Yes, it adds to the mythconverg database. All entries can be removed by executing: “delete from recorded where basename like ‘mmm.%’;”, and then removing any symlinks in your recordings folder “rm mmm.*”, As for deleting, if you have delete follows symlinks on in MythTV, then yes, it will delete. If not, it won’t. Or, if you want to ensure they’re not deleted, just make sure the connection to the share does not have write access.
@JerkyChew: Also, more information on symlinks and deleting can be found here: http://www.mythtv.org/wiki/User_Manual:Detailed_configuration_Backend#Miscellaneous_Settings
Hey guys, with FC12 and Bash v3.2.39 I can’t recreate the issues you’re having, so I need a good test environment to ensure removing the quotes will work in the areas I’m removing them. Can you tell me what distribution and what version of Bash you are running so I can give it a shot there? Thanks.
Ubuntu 9.10
GNU bash, version 4.0.33(1)-release (x86_64-pc-linux-gnu)
In addition to the quote thing (I’m running into it as well, since I’m on ubuntu 9.10) I’ve found that, when importing TV shows, if you have filenames that don’t make it into any of the filters, but those filenames come *after* files that have, that you’ll get lots of weirdness.
For instance, the first file it processes is:
Blah.S01E01.mkv
This is fine; TITLE is Blah, SEASON is 01, and EPISODE is 01.
If the next file it processes is:
Llama.2009.03.28.mkv
The script does not have a handler for that format, and will skip down to inserting. At that point, it uses the prior file’s values and put that show into the database as Blah, 1, 1 (and pulling the appropriate values from TVRage for that).
A simple thing to get around. Just add the following at the top of the insertShow function to initialize things:
TITLE=”";
SEASON=”";
EPISODE=”";
Another problem I’ve had is that I was still getting some shows crossed up. I tracked it down to an import of different shows that had the same season and episode number.
If you look in the insertShow function, and look for:
if [ "$SEARCHRESULT" = "" ]; then
SHOWDATAFILE=”${DATADIR}/showData_${SEARCHTITLE}_${SEASON}x${EPISODE}.tmp”
the next line establishes a name for a temporary storage file. However, it uses a previously undefined variable named ${SEARCHTITLE}. Since it hadn’t been assigned anything it was always blank. So, when the second show with the same season/episode number came along, it reused the existing tmp file.
I’m a newb at bash scripting, so I took a brute force approach, and changed my file to read:
if [ "$SEARCHRESULT" = "" ]; then
RANDOM_VAR=`echo “${FILENAME}” | sed ‘s/ //g’`
SHOWDATAFILE=”${DATADIR}/showData_${RANDOM_VAR}.tmp”
Basically, I yanked all white spaces out of the filename using sed and assigned that to a throw away variable named RANDOM_VAR. Then, I tacked on some other stuff and away it goes.. That should get me unique enough.
Any progress with this or are you waiting to release a new version for when 0.23 comes out?
Hey guys, sorry no progress as of yet as I’ve been traveling constantly. I am in town the first half of this week and will try to push something out.
@JasonFrey: If you’ve got a fixed version of the script and are willing to send it over, I’ll add your name to the credits after I run it through test scenarios and release.
@CaseyBarrett: I will be happy to update for .23 when .23 becomes fully available. If I get a chance to set up a Release Candidate test station I will look into an earlier date.
Is there anyway to add once off TV episodes into the recorings?
@Anthony: I assume you mean an individual file, and as the script is written right now, you could just copy that file into a folder and point the script at the folder. It would require some re-writes to do a single file on the command line.
Yeah thats what I ment, as you mention may just be easier to point the script at the folder in question
Hi there,
Was hoping this script was the end of my issues with MythVideo, however not quite yet Could you support Sickbeard’s naming format? Its not overly configurable im afraid – examples below:
Sleeping 2…
Inserting: Angry Boys – 01×04.avi…
Sleeping 2…
Inserting: Angry Boys – 01×03.avi…
Using spaces is the default, with an option to replace with periods instead of spaces.
Thanks,
iMx
@iMx: Updated to support requested format, see version 0.05.
Can you please change all filepath variables to be double quoted as to support folder names that include spaces
ie. ${TVDIR} becomes “${TVDIR}”
Is it possible to add .flv as a media type?
Thanks!
@Kermitfan22: Done and done. See v0.06.
for subtitle recording rules to take effect you also need to set duplicate = 1 into your SQL
ie. insert into recorded SQL=”(commflagged,filesize,title,subtitle,description,basename,storagegroup,starttime,endtime,originalairdate,progstart,progend,chanid,duplicate) values”
SQL=”$SQL (3,$FILESIZE,’`echo $TITLE |sed “s/’/\\\\\’/g”`’,'`echo $SUBTITLE |sed “s/’/\\\\\’/g”`’,'`echo $DESC |sed “s/’/\\\\\’/g”`’,'`echo mmm.tv.$FILENAME |sed “s/’/\\\\\’/g”`’,'$RECORDINGGROUP’,NOW(),NOW(),’$AIRDATE’,NOW(),NOW(),$CHANID,1);”
You may also wish to insert into oldrecorded to keep track of episodes after they are deleted….
Opps, for subtitle recording rules to take effect you also need to set duplicate = 1 into your SQL
ie. SQL=”insert into recorded (commflagged,filesize,title,subtitle,description,basename,storagegroup,starttime,endtime,originalairdate,progstart,progend,chanid,duplicate) values”
SQL=”$SQL (3,$FILESIZE,’`echo $TITLE |sed “s/’/\\\\\’/g”`’,'`echo $SUBTITLE |sed “s/’/\\\\\’/g”`’,'`echo $DESC |sed “s/’/\\\\\’/g”`’,'`echo mmm.tv.$FILENAME |sed “s/’/\\\\\’/g”`’,'$RECORDINGGROUP’,NOW(),NOW(),’$AIRDATE’,NOW(),NOW(),$CHANID,1);”
You may also wish to insert into oldrecorded to keep track of episodes after they are deleted….
I don’t know about other platforms, but on Ubuntu your dependenceis should list:
imagemagick (instead of convert)
and
mkvtoolnix (instead of mkvinfo & mkvextract)
Would it be possible to support TV file paths like “Show Title\SeasonXX\SXXEXX – Episode Title.xxx” ?
@randomguy: This should already be supported?
The script works great, thanks!
I have a request: is it possible to have the frontend update the “Watch Recordings” screen after the file is inserted into the database?
Currently, I do not see the newly added file until I re-enter the “Watch Recordings” screen and it reloads the recorded list. I know it is possible because the list gets updated when a new recording starts.
Or if you have any ideas how to do it, I can hack at it.
Thanks.
@BobH: Theoretically. I could set up a JS timer object to run an ajax call to the DB every 30 seconds checking for an updated recordings list and refreshing if need. However, I need to think about this more from the standpoint of battery consumption and efficiency.
I love this little shell script… My only complaint is that I can’t delete the shows off of mythtv once I watch them without them showing back up. Can you add an option to not re-add shows. I was thinking you could just add a new table and add a check there before checking the recordings table
I found an issue with your script. All the prime suspect episodes come in wrong. They show up as Prime Suspect (1991) or Prime Suspect 1. Here are some sample file names: Prime.Suspect.(US).S01E06.Gone.to.Pieces.avi Prime.Suspect.(US).S01E02.Carnivorous.Sheep.avi
@Brad: You need to set mythbackend to follow symlinks when deleting. That will delete the file and thus, no re-add. As for the Prime Suspect show, that sounds like a problem with the online tv db, not the filename parsing, so I doubt there’s anything I can do about that.
Updated to version 0.07. See change-log above.
Is there a way to get the script to pull the TV show’s description as well?
I can confirm this still works in 0.25
I seem to have aproblem that the are not falling nicely into the programs classification. All the shows are in the main Programs listing.
Otherwise great work. (I’m on the latest mythtv)
@Brad also make sure your mythtv user has writes to delete the file. I have to do a chmod on the files otherwise it does not delete them.
Sorry it would help if I gave you the insert statement.
echo “insert into recorded (commflagged,filesize,title,subtitle,description,basename,storagegroup,starttime,endtime,originalairdate,progstart,progend,chanid) values (3,183199932,’The Big Bang Theory – 5×22 – The Stag Convergence.avi’,”,’SE: The Big Bang Theory – 5×22 – The Stag Convergence.avi’,'mmm.tv.The Big Bang Theory – 5×22 – The Stag Convergence.avi’,'Default’,NOW(),NOW(),’–’,NOW(),NOW(),2111);”
Shoudl I rename the files to a different format so that the Title gets picked up correctly?
FYI, I started using Mythtv-0.26 where they migrated to storing all date/times in UTC (see http://www.mythtv.org/wiki/UTC ). Thus, in the script all of the times are off when shown in the frontend. My ugly hack was to replace all occurrences of:
NOW()
with
CONVERT_TZ(NOW(), ‘America/Los_Angeles’, ‘UTC’)
Should MythMedia Monitor work with MythTV 0.25?
It should yes, as the DB structure has not been altered. However, I no longer use MythTV and am unable to test it.
Why not use UTC_TIMESTAMP() instead of CONVERT_TZ(NOW(), ‘America/Los_Angeles’, ‘UTC’)