/
btplay
executable file
·118 lines (89 loc) · 3.1 KB
/
btplay
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
#!/usr/bin/env python3
# Copyright 2015 Johan Gunnarsson <johan.gunnarsson@gmail.com>
#
# This file is part of BTFS.
#
# BTFS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BTFS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BTFS. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import sys
import os
import os.path
import tempfile
import shutil
import time
import subprocess
import argparse
extensions = ("mp4", "m4v", "mkv", "avi",
"mpg", "mpeg", "flv", "webm")
players = (("omxplayer", "--timeout", "60"),
("vlc", "--file-caching", "10000"),
("mpv", ),
("mplayer", ),
("totem", ),
("parole", ),
("dragon", ))
def which(x):
for d in os.getenv("PATH", "").split(":"):
if os.path.exists(os.path.join(d, x)):
return os.path.join(d, x)
def find_player(players):
for player in players:
if which(player[0]):
return player
def find_files(filepath):
for dirpath, dnames, fnames in os.walk(filepath):
for f in fnames:
yield os.path.join(dirpath, f)
def is_sample(filename):
return "sample" in os.path.basename(filename).lower()
def is_video(filename):
return any(filename.lower().endswith(i) for i in extensions)
def exit(tempdir, status):
shutil.rmtree(tempdir)
sys.exit(status)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--player",
action="store", help="player to launch")
parser.add_argument("URI", action="store",
help="magnet link or HTTP metadata URL to play")
args = parser.parse_args()
player = find_player([args.player.split()] if args.player else players)
if not player:
print("Could not find a player", file=sys.stderr)
sys.exit(1)
mountpoint = tempfile.mkdtemp(prefix="btplay-")
failed = subprocess.call(["btfs", args.URI, mountpoint])
if failed:
exit(mountpoint, failed)
try:
while not os.listdir(mountpoint):
time.sleep(0.25)
media = sorted(i for i in find_files(mountpoint)
if not is_sample(i) and is_video(i))
if media:
status = subprocess.call(list(player) + media, stdin=sys.stdin)
else:
print("No video media found", file=sys.stderr)
status = 3
except KeyboardInterrupt:
status = 1
except Exception as e:
print("Error:", e, file=sys.stderr)
status = 2
finally:
subprocess.call(["fusermount", "-z", "-u", mountpoint])
exit(mountpoint, status)
if __name__ == '__main__':
main()