File: example.py

package info (click to toggle)
clapper 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 3,540 kB
  • sloc: ansic: 25,139; python: 524; xml: 496; javascript: 18; sh: 10; makefile: 5
file content (57 lines) | stat: -rwxr-xr-x 2,188 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3

import gi
gi.require_version('Adw', '1')
gi.require_version('Clapper', '0.0')
gi.require_version('ClapperGtk', '0.0')
gi.require_version('GLib', '2.0')
gi.require_version('Gtk', '4.0')
from gi.repository import Adw, Clapper, ClapperGtk, GLib, Gtk
import shutil

Clapper.init(None)

download_dir = GLib.build_filenamev([GLib.get_user_cache_dir(), "example_download_dir", None])
print('Using cache directory: {0}'.format(download_dir))

def on_download_complete(player, item, location):
    # Media downloaded. Data from this file is still used for current playback (including seeking).
    print('Download complete: {0} => {1}'.format(item.props.uri, location))

def on_activate(app):
    win = Gtk.ApplicationWindow(application=app, default_width=640, default_height=396)
    video = ClapperGtk.Video()
    controls = ClapperGtk.SimpleControls(fullscreenable=False)

    # Enable local storage caching and monitor it
    video.props.player.set_download_dir(download_dir)
    video.props.player.set_download_enabled(True)
    video.props.player.connect('download-complete', on_download_complete)

    # Configure playback
    video.props.player.props.queue.set_progression_mode(Clapper.QueueProgressionMode.CAROUSEL)
    video.props.player.set_autoplay(True)

    # Assemble window
    video.add_fading_overlay(controls)
    win.set_child(video)
    win.present()

    # Create and add media for playback
    item_1 = Clapper.MediaItem(uri='http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4')
    item_2 = Clapper.MediaItem(uri='http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4')
    video.props.player.props.queue.add_item(item_1)
    video.props.player.props.queue.add_item(item_2)

# Create a new application
app = Adw.Application(application_id='com.example.ClapperDownloadCache')
app.connect('activate', on_activate)

# Run the application
app.run(None)

# Finally app should cleanup before exit. Possibly moving data to
# another dir if it wants to use it on next run and deleting what's
# left (so any unfinished downloads will also be removed).
print('Cleanup')
shutil.rmtree(download_dir)