~photostory/photostory/cleanup

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/python

#Photostory 0.9
#by Joel Auterson (joel.auterson@googlemail.com)
#http://www.launchpad.net/photostory

import pygst
pygst.require("0.10")
import gst
import pygtk
import gtk
from time import strftime
import time
import os.path
import os
import cPickle

class Main:


    def __init__(self):

        pipeline = None
        xvimagesink = None
        movie = None
        self.pic=None
        self.db = cPickle.load(open('data/db', 'rb'))
        self.ind = int(cPickle.load(open('data/num', 'rb')))
        todayPicName = "pictures/" + str(self.ind) + ".png"
        todayDate = None

        def chooseDay(cal):
            dateTuple = cal.get_date()
            setPic(dateTuple)

        def closedown(win):
            print 'ind ' + str(self.ind)
            print 'db' + str(self.db)
            cPickle.dump(self.db, open('data/db', 'wb'))
            cPickle.dump(self.ind, open('data/num', 'wb'))
            gtk.main_quit()

        def about(aboutBut):
            dAbout = gtk.AboutDialog()
            dAbout.set_name("Photostory")
            dAbout.set_comments("Photostory is an application that lets you tell the story of your life in pictures, by taking a snapshot of you each day. You can then make these into a video to share with friends or on the internet.")
            response = dAbout.run()
            if response == -6:
                dAbout.destroy()

        def movify(filmBut):

            def movPick(movFileButton):
                movPicker = gtk.FileChooserDialog(title="Choose a save location", parent=movDia, action=gtk.FILE_CHOOSER_ACTION_SAVE, buttons=(("Save Here", -6)), backend=None)
                movRes = movPicker.run()
                if movRes == -6:
                    movPath = movPicker.get_filename()
                    movPicker.destroy()

            def movGen(movButton):

                movDia.destroy()

                filmPipe = gst.Pipeline("filmPipe")
                filmSrc = gst.element_factory_make("multifilesrc", "filmSrc")
                filmSrc.set_property("location", "pictures/%d.png")
                filmFilt1 = gst.element_factory_make("capsfilter", "filmFilt1")
                filmCap1 = gst.Caps("image/png,framerate=5/1,pixel-aspect-ratio=1/1")
                filmFilt1.set_property("caps", filmCap1)
                filmPngDec = gst.element_factory_make("pngdec", "filmPngDec")
                filmff = gst.element_factory_make("ffmpegcolorspace", "filmff")
                filmFilt2 = gst.element_factory_make("capsfilter", "filmFilt2")
                filmCap2 = gst.Caps("video/x-raw-yuv")
                filmFilt2.set_property("caps", filmCap2)
                filmTheora = gst.element_factory_make("xvidenc", "filmTheora")
                filmOggmux = gst.element_factory_make("ffmux_mp4", "filmOggmux")
                filmFilesink = gst.element_factory_make("filesink", "filmFilesink")
                filmFilesink.set_property("location", movPath)

                filmPipe.add(filmSrc, filmFilt1, filmPngDec, filmff, filmFilt2, filmTheora, filmOggmux, filmFilesink)
                gst.element_link_many(filmSrc, filmFilt1, filmPngDec, filmff, filmFilt2, filmTheora, filmOggmux, filmFilesink)
                filmPipe.set_state(gst.STATE_PLAYING)
                time.sleep(5)
                filmBut.set_sensitive(True)

            movDia = gtk.Window(gtk.WINDOW_TOPLEVEL)
            movDia.set_title("Create Film")
            movDia.set_resizable(False)
            filmBut.set_sensitive(False)
            movVbox = gtk.VBox(homogeneous=False, spacing=2)
            movLabel = gtk.Label("Here you can create a video made up of all your pictures. \n\nJust choose a save location and hit 'create'.\n\nRemember, the path must end in '.mp4'.\n")
            movFileButton = gtk.Button(label="Choose a location")
            movButton = gtk.Button(label="Create")
            movDia.add(movVbox)
            movVbox.pack_start(movLabel, expand=False)
            movVbox.pack_start(movFileButton, expand=False)
            movVbox.pack_start(movButton, expand=False)
            movButton.connect("clicked", movGen)
            movFileButton.connect("clicked", movPick)
            movDia.show_all()

        def capture(takeBut):
            pipeline.set_state(gst.STATE_NULL)
            stillPipe = gst.Pipeline("stillPipe")
            stillCam = gst.element_factory_make("v4l2src", "stillPipe")
            stillFilt = gst.element_factory_make("capsfilter", "stillFilt")
            stillCap = gst.Caps("video/x-raw-yuv,width=640,height=480")
            stillFilt.set_property("caps", stillCap)
            ffmpegcolorspace = gst.element_factory_make("ffmpegcolorspace", "ffmpegcolorspace")
            pngEnc = gst.element_factory_make("pngenc", "pngenc")
            filesink = gst.element_factory_make("filesink", "filesink")
            filesink.set_property("location", todayPicName)
            self.db[todayDate] = todayPicName
            cal.freeze()
            cal.select_month(todayDate[1], todayDate[0])
            cal.select_day(todayDate[2])
            cal.thaw()
            stillPipe.add(stillCam, stillFilt, ffmpegcolorspace, pngEnc, filesink)
            gst.element_link_many(stillCam, stillFilt, ffmpegcolorspace, pngEnc, filesink)
            stillPipe.set_state(gst.STATE_PLAYING)
            time.sleep(1)
            stillPipe.set_state(gst.STATE_NULL)
            xvimagesink.set_xwindow_id(movie.window.xid)
            pipeline.set_state(gst.STATE_PLAYING)
            setPic(todayDate)
            self.ind += 1
            
        def deletePic(deleteBut):
            def getKey(dic, val):
                return [k for k, v in dic.iteritems() if v == val][0]
            date = cal.get_date()
            if date in self.db:
                os.remove(self.db[date])
                setPic("")
                if date == todayDate:
                    takeBut.set_label("Take today's picture")
                    takeBut.set_sensitive(True)
                gaps = 0
                i = 0
                for element in self.db:
                    oldPath = "pictures/" + str(i) + ".png"
                    if not os.path.exists(oldPath):
                        gaps += 1
                        i += 1
                    oldPath = "pictures/" + str(i) + ".png"
                    if os.path.exists(oldPath):
                        key = getKey(self.db, oldPath)
                        newPath = "pictures/" + str(i-gaps) + ".png"
                        os.rename(oldPath, newPath)
                        self.db[key] = newPath
                    i += 1
                del self.db[date]
                self.ind -= 1

        def setPic(date):
            if self.pic != None:
                vbox1.remove(self.pic)
                vbox1.remove(hbox2)
            if date in self.db:
                self.pic = gtk.Image()
                self.pic.set_from_file(self.db[date])
                if date == todayDate:
                    takeBut.set_label("Picture taken for this day.")
                    takeBut.set_sensitive(False)
            else: 
                self.pic = gtk.Label()
                self.pic.set_justify(gtk.JUSTIFY_CENTER)
                self.pic.set_markup("<span size='54000'>No Picture\nToday</span>");
                self.pic.set_size_request(640, 480)
            vbox1.pack_start(self.pic)
            vbox1.pack_start(hbox2)
            vbox1.show_all()

        #Interface
        win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        win.connect("destroy", closedown)
        win.set_default_size(900, 600)
        win.set_resizable(False)
        win.set_title("Photostory")
        win.set_icon_from_file("data/icon.svg")
        movie = gtk.DrawingArea()
        hbox = gtk.HBox(homogeneous=False, spacing=3)
        vbox1 = gtk.VBox(homogeneous=False)
        vbox2 = gtk.VBox(homogeneous = False)   
        filmBut = gtk.Button(label="Create Film")
        filmBut.connect("clicked", movify)
        deleteBut = gtk.Button(label="Delete Picture")
        deleteBut.connect("clicked", deletePic)
        shareBut = gtk.Button(label="Share Video")
        hbox2 = gtk.HBox(homogeneous=True)
        aboutBut = gtk.Button(label="About")
        aboutBut.connect("clicked", about)
            
        cal = gtk.Calendar()
        todayDate = cal.get_date()
        cal.connect("day-selected", chooseDay)
        
        takeBut = gtk.Button(label="Take today's picture")
        takeBut.connect("clicked", capture)
        
        setPic(todayDate)
        
        win.add(hbox)
        hbox.pack_start(vbox1, expand=False)
        hbox.pack_start(vbox2)
        hbox2.pack_start(filmBut, expand=False)
        hbox2.pack_start(deleteBut, expand=False)
        #hbox2.pack_start(shareBut)
        hbox2.pack_start(aboutBut)
        hbox.pack_start(vbox2)
        vbox2.pack_start(movie)
        vbox2.pack_start(cal, expand=False, padding=50)
        vbox2.pack_start(takeBut, expand=False)

        win.show_all()

        #Pipeline stuff - feed
        pipeline = gst.Pipeline("mypipeline")

        camera = gst.element_factory_make("v4l2src", "camera")
        camera.set_property("device", "/dev/video0")
        
        caps = gst.Caps("video/x-raw-yuv,width=640,height=480,framerate=30/1")
        filt = gst.element_factory_make("capsfilter", "filter")
        filt.set_property("caps", caps)

        xvimagesink = gst.element_factory_make("xvimagesink", "sink")

        pipeline.add(camera, filt, xvimagesink)
        gst.element_link_many(camera, filt, xvimagesink)

        xvimagesink.set_xwindow_id(movie.window.xid)

        pipeline.set_state(gst.STATE_PLAYING)

start=Main()
gtk.main()