Auto adjust display refresh rate - Need help with a script
#1
Star 
It seems that native support for adjusting refresh according to video in XBMC on OS X is a long way off.

While we wait for that, I was wondering if maybe we could make a script that accomplishes the same.

First, let me say that I'm not a programmer, but maybe we can work together on making a script for auto adjusting display refresh on OS X.

Let's do a brainstorm in this thread and see if we, together, can make a working script.

-----

My thoughts so far:

I have considered a script like YouTrailer (or Home Theater Experience) where a button on the remote (or in the skin GUI) executes the script, which performs some commands, before starting playback of the selected video.

The script would:

1. Get the file path for the selected video (in the XBMC DB?)
2. Pass the file path to MediaInfo CLI for OS X
3. Grab the "framerate" info from the MediaInfo result
4. Call the correct display refresh change in SwitchResX, based on the framerate info
5. Start playback of the selected video

I considered "cscreen" or "ScrUtil" for doing the display refresh change directly, without SwitchResX, but I'm not sure that those two programs can do "23.976 Hz" (which IMO is very important, as many movies use that framerate and 24 Hz with a 23.976 Hz video would present judder).

With SwitchResX you can assign shortcuts to selected resolutions and just call the appropriate shortcut in the script.

I have used this already with my Harmony remote, where I assigned a button to execute a small script which only job was to do a virtual "F4" keyboard command, for example, which was assigned to the correct resolution in SwitchResX.

But to automate this behaviour, and let the script figure out what the fps of the video is, would be a lot simpler solution.

Another thing to consider is how we can switch back the resolution/refresh when stopping a video.

In XBMC on Linux the resolution switches back to what it was before starting playback, when you stop a video.

Maybe, in the Python code, the script can check if video playback is active and when it detects a stop in video playback it changes the resolution back.
Reply
#2
Here is some code from the YouTrailer script.

Again, I'm not a programmer but maybe this is a place to start Smile

Could we rewrite "fetchXbmcData" to get the file path of the selected video and "playTrailer" to start playback of the video?

Code:
import os, sys, time, re
import xbmc
import xbmcgui
from pysqlite2 import dbapi2 as sqlite
import tmdb, traceback
import urllib, urllib2
from cgi import parse_qs

_lang = xbmc.Language(os.getcwd()).getLocalizedString

DBPATH = xbmc.translatePath( "special://database/MyVideos34.db" )
__scriptname__ = sys.modules[ "__main__" ].__scriptname__
__version__ = sys.modules[ "__main__" ].__version__
__credits__ = sys.modules[ "__main__" ].__credits__

BASE_DATABASE_PATH = os.path.join( xbmc.translatePath( "special://profile/" ), "script_data", __scriptname__, "YouTrailer.db" )

DB_KEYS = {
    'Title' : 'name',
    }


SETTINGS = sys.modules[ "__main__" ].XBMC_SETTINGS

#User Settings

SLEEP_TIME = 5 #in seconds to check if trailer playing after play command sent


class Main:
    def __init__(self):
        self.log( "Main Started" )
        starttime = time.time()
        self.setupVariables()
        self.fetchInfoLabels()
        self.main()
        while( time.time() - starttime < 5 ):
            time.sleep( 0.5 )


    def log( self, string ):
        xbmc.log( "[SCRIPT] '%s: version %s' %s" % ( __scriptname__, __version__, string ), xbmc.LOGNOTICE )
        return string


    def main( self ):
        try:
            self.progress_dialog.create( __scriptname__, '' )
            self.fetchXbmcData()
            self.getTrailer()
            self.playTrailer()
            self.progress_dialog.close()
            xbmc.executebuiltin('Dialog.Close(MovieInformation)')
            self.validChecker()
        except:
            traceback.print_exc()
            self.progress_dialog.close()
            self.log( "Script Error: %s" % _lang( self.error_id ) )
            self.dialog.ok( "%s - %s" % ( __scriptname__, _lang( 30 ) ), "", "", _lang( self.error_id ) )
        self.log( "Credits: %s" % __credits__)
        self.log( "Script Complete" )



    def setupVariables( self ):
        self.log( "Setting Up Variables" )
        self.player = xbmc.Player()
        self.progress_dialog = xbmcgui.DialogProgress()
        self.dialog = xbmcgui.Dialog()
        self.error_id = 35
        self.settings = {
                        'STime' : SLEEP_TIME,
                        }
        self.movie_info = {
                           'Title': '',
                           'Director' : '',
                           'Year' : '',
                           'Genre' : '',
                           'Thumb' : '',
                           'Imdb' : '',
                           'Tmdb' : '',
                           'Url' : '',
                           'Local' : '',
                           'Full' : '',
                           'Tid' : ''
                            }
        print self.settings['LatTrailer']


    def fetchInfoLabels( self ):
        self.movie_info['Title'] = xbmc.getInfoLabel( 'listitem.title' )      


    def fetchXbmcData( self ):
        self.error_id = 31 #Failed To Retrieve XBMC DB Movie Info
        try:
            self.log( "Fetching XBMC Data" )
            db = sqlite.connect( DBPATH )
            cursor = db.cursor()
            cursor.execute( "SELECT c09, c19 FROM movie WHERE c00 = ?;", ( self.movie_info['Title'] ) )
            xbmc_data = cursor.fetchone()
            self.movie_info['Imdb'] = xbmc_data[0].strip()
            self.movie_info['Url'] = xbmc_data[1].strip()
            if( self.movie_info['Imdb'] == '' or self.movie_info['Imdb'] == None ):
                raise
            db.close()
        except:
            self.log( "ERROR Fetching XBMC Data" )
            raise  

        
    def playTrailer( self ):
        self.log( "Playing Trailer: %s" % self.movie_info['Full'] )
        self.error_id = 34
        try:
            self.progress_dialog.update( 100, '%s %s' % ( self.movie_info['Title'], _lang( 2 ) ), "", _lang( 14 ) )            
            listitem = xbmcgui.ListItem( self.movie_info['Title'], thumbnailImage = self.movie_info['Thumb'] )
            listitem.setInfo( 'video', {'Title': '%s %s' % ( self.movie_info['Title'], _lang( 2 ) ), 'Studio' : __scriptname__, 'Genre' : self.movie_info['Genre'] } )
            self.player.play( self.movie_info['Full'], listitem )
        except:
            self.log( "ERROR Playing Trailer: %s" % self.movie_info['Full'] )
            raise
            
Main()

In between those two we would need the MediaInfo code to determine the correct framerate and call the SwitchResX shortcut to change the refresh.

Here is some code I found (a bash script), maybe we can use some of it:
Code:
#!/bin/bash

######## Path to MediaInfo ##########
mediainfo="/usr/local/bin/mediainfo"
#####################################

################ Ignore upper and lowercase ###############
shopt -s nocaseglob
###########################################################

##### Parse file to MediaInfo CLI #####
for i in *.mkv; do #<-- CHANGE ME - parse video from XBMC here#
fps=`/usr/local/bin/mediainfo --Inform=Video\;%FrameRate% "$i"`
###########################################################

### Looking for the FPS and initiate the display res change ####
if  [ "$fps" = "23.976" ]; then
    #execute the correct script that calls the SwitchResX refresh change#
    done

elif  [ "$fps" = "24.000" ]; then
    #execute the correct script that calls the SwitchResX refresh change#
    done

elif  [ "$fps" = "29.970" ]; then
    #execute the correct script that calls the SwitchResX refresh change#
    done

fi
###########################################################

done
Reply
#3
long time no answer but i have exactly the same prob. could you please tell me how you get the harmony working to switch the refresh rates with a buttonpress in mac os x? im really not familiar with apple and use the mac mini only for xbmc. yet i always go to the settings and switch the refresh rate from 60hz (for watching series) to 24hz (blurayrips) and back.
-= XBMC Lover © 2006 =-
---------------------------------------
XBMC @ Lenovo Q180, harmony one, Sony Bravia KDL-55W905, Marantz SR5006, MySQL XBMC Database @ Synology DS-409+ (thx Firnsy)
XBMC @ Lenovo Q150 and a Panasonic Plasma for bedrooming ;)
Reply

Logout Mark Read Team Forum Stats Members Help
Auto adjust display refresh rate - Need help with a script0