Start Random Music Script Provided

Discussion of XBMC4XBOX development.
Post Reply
Dogfyter
Posts: 28
Joined: Mon Jul 23, 2012 11:51 pm
Has thanked: 6 times

Start Random Music Script Provided

Post by Dogfyter »

Dear XBMC4XBOX,

Here is a simple script I found years ago for playing your own music files.

''' vim: ts=8 sw=4 noexpandtab
Author: Rune Hammersland
version: 0.3

Xbox Media Center comes with a script to play an mp3 file at startup
(StartUpMP3). This script will load a playlist if it exists, and if not,
traverse a directory and load all relevant music files. It is intended as a
replacement for StartUpMP3, and enables you to listen to a playlist, or all
music files in a given directory.

Changelog:
2005-05-27 13:10:33 - Rune Hammersland:
* Added possibility for multiple playlists.
* Added possibility for multiple music dirs.

'''

import xbmc
import os

# ---------------------------------------- #
# "Configuration"
# ---------------------------------------- #

# Change this to your playlist(s).
# Written like this: ['path\\to\\plist1', 'to\\plist2', 'plist\\3']
playlist_files= ['E:\\Media\\Music\\playlist.m3u', 'G:\\all.m3u']
# Dirs(s) to traverse if playlist does not exist. NB: No trailing slash.
# Written like this: ['path\\to\\dir1', 'to\\dir2', 'dir\\3']
music_dirs = ['E:\\Music', 'F:\\Music', 'G:\\Music']
# Shuffle playlist if this var equals 1.
shuffle_files = 1


# ---------------------------------------- #
# Code
# ---------------------------------------- #

# Function for adding files to playlist
def add_files(pl, dirname, names):
for filename in names:
if (os.path.isfile(dirname + "\\" + filename)):
add = 0

# Check extension of file.
if (filename[-4:] == ".mp3"): add = 1
if (filename[-4:] == ".ogg"): add = 1
if (filename[-4:] == ".wav"): add = 1
if (filename[-5:] == ".flac"): add = 1

# If file is to be added, do it.
if (add == 1): pl.add(dirname + "\\" + filename)
elif (os.path.isdir(dirname + "\\" + filename)):
os.path.walk(dirname + "\\" + filename, add_files, pl)

# Get music playlist from XBMC
plist = xbmc.PlayList(0)
plist.clear()

# Load playlist if it exists
for playlist_file in playlist_files:
if (os.path.isfile(playlist_file)):
plist.load(playlist_file)
# Else, find all available music
else:
for music_dir in music_dirs:
os.path.walk(music_dir, add_files, plist)

# Do the shuffle!
if (shuffle_files == 1): plist.shuffle()

xbmc.Player().play(plist)


You might want to place it in its own folder. I use this with another old script called "Auto Start Manager". These two scripts combined start random music from my MUSIC folders at BOOT. I am more than willing to share the "Auto Start Manager" script but, this script has a number of items in its folder and I do not know how to provide a folder full of items to you.

I did not write either script but, leave all credit to their authors. I hope there are those who will enjoy the startup music script.

I am curious to know if there is a way to incorporate the code from the Music script into XBMC. The "Playlist" option is ok but, not practicle if you have thousands of songs you have to add to the playlist every time you add music to your inventory. I am hoping this could be a simpler solution for those of us who have large hdds full of music files. Thanks in advance for your consideration.

Dogfyter
User avatar
xman
Posts: 1289
Joined: Wed Jul 04, 2012 2:30 pm
Location: Sydney, Australia
Has thanked: 55 times
Been thanked: 168 times

Re: Start Random Music Script Provided

Post by xman »

Random play I would love. ResumeX allows your list to continue from where it stopped the last time which I find good and it works with music videos as well. Problem is it isn't compatible with the latest builds of XBMC. You'll need to go back in builds till you find where exactly but on my machine I have this script and that machine is my main music machine, it is running a 2011 build of XBMC. The actual cutoff build is actually much later but this build runs sweet on this machine for what it does so it can stay that way.
Dogfyter
Posts: 28
Joined: Mon Jul 23, 2012 11:51 pm
Has thanked: 6 times

Re: Start Random Music Script Provided

Post by Dogfyter »

Xman, thanks for the response. I will look into ResumeX. Does it start at boot? In either case this music script works fine with the current builds. I have recently changed the code to allow for .wmv files instead of .ogg's. This allowed me to add E:\\TDATA\\fffe0000\\music\\0000 to the scripts resources. This where the M$ dash puts the ripped songs.

Again, the "Auto Start Manager" script auto starts any script or file on boot. But, it is a small folder with a few files. If you are interested, let me know how to share this small folder.
Dogfyter
Posts: 28
Joined: Mon Jul 23, 2012 11:51 pm
Has thanked: 6 times

Re: Start Random Music Script Provided

Post by Dogfyter »

I mean .WMA files instead of .WMV's. LOL
User avatar
xman
Posts: 1289
Joined: Wed Jul 04, 2012 2:30 pm
Location: Sydney, Australia
Has thanked: 55 times
Been thanked: 168 times

Re: Start Random Music Script Provided

Post by xman »

Dogfyter wrote: I will look into ResumeX. Does it start at boot?
Yes, exactly like a startup file does only it continues on it rather than start from the beginning. The only place I can say you will get it is here-
http://7tft.wordpress.com/download/
You need to download the skin 7TFT and the ResumeX script is in the Extras folder inside this skin's folders. It is called "ResumeX" I also think it is a script on the AID disc but I'm not certain about that. It is definitely in this skin.
Dogfyter
Posts: 28
Joined: Mon Jul 23, 2012 11:51 pm
Has thanked: 6 times

Re: Start Random Music Script Provided

Post by Dogfyter »

Xman, thanks again. I got ResumeX to work on a current build. I have it running on a new pm3 skin. Also because of this, my noob ass has discovered that all you need to get the script I provided to work on boot is to point the autoexec.py (located in the scripts folder) to the script you want to start on boot. My autoexec.py looks like this;


# auto execute scripts when xbmc starts, place this file in xbmchome\scripts\
#
# note: - do not execute more than one script at a time which asks for user input!

import xbmc

xbmc.executescript('Q:\\scripts\\startup music\\default.py')


As you can tell my script is in a folder called "startup music". The ResumeX is for playlists. Which is fine but, any time I want to add a new song to my "Music" folder I also have to add it to my playlist. I guess this is alright?

I have just become accustomed to having any new added songs automatically kicked into a random playlist generated at boot by the script.
Post Reply