~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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/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
from threading import Timer
import glib

class Main:

    def __init__(self):

        pipeline = None
        xvimagesink = None
        movie = None
        self.movPath = None
        self.adj = gtk.Adjustment(5, 1, 10, 1)
        self.pic=None

        # If the `~/.photostory/photos` directory doesn't exist, create it:
        if not os.path.exists(os.path.expanduser('~/.photostory/photos')):
            os.makedirs(os.path.expanduser('~/.photostory/photos'))

        # If the `~/.photostory/db` pickle doesn't exist, create it:
        if not os.path.isfile(os.path.expanduser('~/.photostory/db')):
            cPickle.dump({}, open(os.path.expanduser('~/.photostory/db'), 'w'))

        # If the `~/.photostory/num` pickle doesn't exist, create it:
        if not os.path.isfile(os.path.expanduser('~/.photostory/num')):
            cPickle.dump(0, open(os.path.expanduser('~/.photostory/num'), 'w'))

        # Load the `db` and `num` pickles:
        self.db = cPickle.load(open(os.path.expanduser('~/.photostory/db'), 'rb'))
        self.ind = int(cPickle.load(open(os.path.expanduser('~/.photostory/num'), 'rb')))

        todayPicName = os.path.expanduser('~/.photostory/photos/') + 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(os.path.expanduser('~/.photostory/db'), 'wb'))
            cPickle.dump(self.ind, open(os.path.expanduser('~/.photostory/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 photos, by taking a snapshot of you each day. You can then make these into a video to share with friends or on the internet.")
            dAbout.set_artists(("Josh Brown", ""))
            dAbout.set_authors(("Joel Auterson", "David Turner", "Josh Brown"))
            dAbout.set_website("http://launchpad.net/photostory")
            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:
                    self.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", os.path.expanduser("~/.photostory/photos/%d.png"))
                filmFilt1 = gst.element_factory_make("capsfilter", "filmFilt1")
                filmCap1 = gst.Caps("image/png,framerate=" + str(self.adj.get_value()) + "/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", self.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 photos. \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")

            #FPS Slider
            movSliderBox = gtk.HBox(homogeneous=False, spacing=3)
            movSliderLabel = gtk.Label("FPS:")
            movSlider = gtk.HScale(self.adj)
            movSlider.set_digits(0)
            movSliderBox.pack_start(movSliderLabel, expand=False)
            movSliderBox.pack_start(movSlider, expand=True)

            movDia.add(movVbox)
            movVbox.pack_start(movLabel, expand=False)
            movVbox.pack_start(movSliderBox, 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):

            def takePic():
                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 countdown(n = 3):
                self.pic.set_markup("<span size='54000'>" + str(n) + "</span>")
                if n == 0: takePic()
                else: glib.timeout_add(1000, countdown, n-1)

            countdown()

        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 photo")
                    takeBut.set_sensitive(True)
                gaps = 0
                i = 0
                for element in self.db:
                    oldPath = os.path.expanduser('~/.photostory/photos/') + str(i) + ".png"
                    if not os.path.exists(oldPath):
                        gaps += 1
                        i += 1
                    oldPath = os.path.expanduser('~/.photostory/photos/') + str(i) + ".png"
                    if os.path.exists(oldPath):
                        key = getKey(self.db, oldPath)
                        newPath = os.path.expanduser('~/.photostory/photos/') + 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])
                takeBut.set_label("Photo taken for this day.")
                takeBut.set_sensitive(False)
            else: 
                if date == todayDate:
                    takeBut.set_label("Take today's photo")
                    takeBut.set_sensitive(True)
                else:
                    takeBut.set_label("Take today's photo")
                    takeBut.set_sensitive(False)
                self.pic = gtk.Label()
                self.pic.set_justify(gtk.JUSTIFY_CENTER)
                self.pic.set_markup("<span size='54000'>No Photo\nToday</span>");
                self.pic.set_size_request(640, 480)
            vbox1.pack_start(self.pic)
            vbox1.pack_start(hbox2)
            vbox1.show_all()

        #Interface
        self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.win.connect("destroy", closedown)
        self.win.set_default_size(900, 600)
        self.win.set_resizable(False)
        self.win.set_title("Photostory")
        self.win.set_icon_from_file("photostory.svg")
        movie = gtk.DrawingArea()
        movie.set_size_request(320, 240)
        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 Photo")
        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 photo")
        takeBut.connect("clicked", capture)

        setPic(todayDate)

        self.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=25)
        vbox2.pack_start(takeBut, expand=False)

        self.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()