~luisbg/freemix/head

« back to all changes in this revision

Viewing changes to tools/shuffle.py

  • Committer: Luis de Bethencourt
  • Author(s): Luis de Bethencourt
  • Date: 2009-03-30 15:46:09 UTC
  • Revision ID: luisbg@cerberus-20090330154609-7y12eil911k1nsif
added first tool: shuffle

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (C) 2008 Luis de Bethencourt
 
4
# <luisbg@ubuntu.com>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2, or (at your option)
 
9
# any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 
19
# USA
 
20
 
 
21
 
 
22
"""play videos randomly"""
 
23
 
 
24
import sys, os
 
25
sys.path.append("../src")
 
26
 
 
27
from engine import Engine
 
28
from controller import Controller
 
29
 
 
30
import time
 
31
import optparse
 
32
import random
 
33
 
 
34
class Tool:
 
35
    def __init__(self):
 
36
        usage = """shuffle.py [input_folder]"""
 
37
        parser = optparse.OptionParser(usage = usage)
 
38
        (options, args) = parser.parse_args()
 
39
 
 
40
        if len(args) == 0:
 
41
            print "A video folder needs to be specified."
 
42
            exit()
 
43
        else:
 
44
            self.folder = args[0]
 
45
            if self.folder.endswith("/"):
 
46
                self.folder = self.folder[:-1]
 
47
 
 
48
        engine = Engine()
 
49
        controller = Controller(engine)
 
50
 
 
51
        self.tree = []
 
52
        ls = os.listdir(self.folder)
 
53
 
 
54
        # Populating database of files in input directory.
 
55
        self.browse_folder(ls, self.folder, self.tree)
 
56
 
 
57
        length = len(self.tree)
 
58
        print ""
 
59
        print "Founded " + str(length) + " videos."
 
60
        print ""
 
61
 
 
62
        while True: 
 
63
            video = random.choice(self.tree)
 
64
            controller.play(video, 1.0, 0)
 
65
            print "playing " + video
 
66
            time.sleep(3)
 
67
 
 
68
    def browse_folder(self, folder, dir, tree):
 
69
        '''Creates a tree of all the files and folder inside the in    put dir.'''
 
70
 
 
71
        for files in folder:
 
72
            if not files.startswith(".") and (not files == folder):
 
73
                files = dir + "/" + files
 
74
                tree.append(files)
 
75
                if os.path.isdir(files):
 
76
                    temp = files[files.find(self.folder_short):]
 
77
                    temp = "".join("%s/" % (n) for n in \
 
78
                    temp.split("/")[1:])
 
79
                    new = os.listdir(files)
 
80
                    self.browse_folder(new, files, tree)
 
81
        
 
82
 
 
83
if __name__ == "__main__":
 
84
    try:
 
85
        print "Welcome to freemix shuffle... enjoy!"
 
86
        tool = Tool()
 
87
 
 
88
    except SystemExit:
 
89
        raise