This repository was archived by the owner on Aug 18, 2025. It is now read-only.
forked from vincelwt/RaspberryCast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
executable file
·139 lines (112 loc) · 4.45 KB
/
process.py
File metadata and controls
executable file
·139 lines (112 loc) · 4.45 KB
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
import youtube_dl
import os, threading, logging, json
with open('raspberrycast.conf') as f:
config = json.load(f)
logger = logging.getLogger("RaspberryCast")
def launchvideo(url, sub=False):
setState("2")
os.system("echo -n q > /tmp/cmd &") #Kill previous instance of OMX
if config["new_log"]:
os.system("sudo fbi -T 1 -a --noverbose images/processing.jpg")
logger.info('Extracting source video URL...')
out = return_full_url(url, sub)
logger.info("Full video URL fetched.")
thread = threading.Thread(target=playWithOMX, args=(out, sub,))
thread.start()
os.system("echo . > /tmp/cmd &") #Start signal for OMXplayer
def queuevideo(url, onlyqueue=False):
logger.info('Extracting source video URL, before adding to queue...')
out = return_full_url(url, False)
logger.info("Full video URL fetched.")
if getState() == "0" and onlyqueue == False:
logger.info('No video currently playing, playing video instead of adding to queue.')
thread = threading.Thread(target=playWithOMX, args=(out, False,))
thread.start()
os.system("echo . > /tmp/cmd &") #Start signal for OMXplayer
else:
if out is not None:
with open('video.queue', 'a') as f:
f.write(out+'\n')
def return_full_url(url, sub=False):
logger.info("Parsing source url for "+url+" with subs :"+str(sub))
if (url[-4:] in (".avi", ".mkv", ".mp4", ".mp3")) or (sub) or (".googlevideo.com/" in url):
logger.info('Direct video URL, no need to use youtube-dl.')
return url
slow = config["slow_mode"]
logger.info('Slow mode: ' + str(slow))
if slow:
logger.info('CASTING: Slow mode detected, getting best format <= 480p')
ydl_options = {
'logger': logger,
'noplaylist': True,
'ignoreerrors': True,
'format': 'mp4[height<=480]/best[height<=480]'}
else:
logger.info('CASTING: Getting best format')
ydl_options = {
'logger': logger,
'noplaylist': True,
'ignoreerrors': True,
'format': 'mp4/best'}
ydl = youtube_dl.YoutubeDL(ydl_options) # Ignore errors in case of error in long playlists
with ydl: #Downloading youtub-dl infos
result = ydl.extract_info(url, download=False) #We just want to extract the info
if result is None:
logger.error("Result is none, returning none. Cancelling following function.")
return None
if 'entries' in result: #Can be a playlist or a list of videos
video = result['entries'][0]
else:
video = result #Just a video
#with the format option set above, we should have to worry about parsing for a specific format, it should already pull the right one.
logger.debug("Selected format: " + video['resolution'] + " " + video['ext'])
return video['url']
def playlist(url, cast_now):
logger.info("Processing playlist.")
if cast_now:
logger.info("Playing first video of playlist")
launchvideo(url) #Launch first vdeo
else:
queuevideo(url)
thread = threading.Thread(target=playlistToQueue, args=(url,))
thread.start()
def playlistToQueue(url):
logger.info("Adding every videos from playlist to queue.")
ydl = youtube_dl.YoutubeDL({'logger': logger, 'extract_flat': 'in_playlist', 'ignoreerrors': True})
with ydl: #Downloading youtub-dl infos
result = ydl.extract_info(url, download=False)
for i in result['entries']:
logger.info("queuing video")
if i != result['entries'][0]:
queuevideo(i['url'])
def playWithOMX(url, sub):
logger.info("Starting OMXPlayer now.")
setState("1")
if sub:
os.system("omxplayer -b -r -o both '" + url + "' --subtitles subtitle.srt < /tmp/cmd")
elif url is None:
pass
else :
os.system("omxplayer -b -r -o both '" + url + "' < /tmp/cmd")
if getState() != "2": # In case we are again in the launchvideo function
setState("0")
with open('video.queue', 'r') as f: #Check if there is videos in queue
first_line = f.readline().replace('\n', '')
if first_line != "":
logger.info("Starting next video in playlist.")
with open('video.queue', 'r') as fin:
data = fin.read().splitlines(True)
with open('video.queue', 'w') as fout:
fout.writelines(data[1:])
thread = threading.Thread(target=playWithOMX, args=(first_line, False,))
thread.start()
os.system("echo . > /tmp/cmd &") #Start signal for OMXplayer
else:
logger.info("Playlist empty, skipping.")
if config["new_log"]:
os.system("sudo fbi -T 1 -a --noverbose images/ready.jpg")
def setState(state):
os.system("echo "+state+" > state.tmp") #Write to file so it can be accessed from everywhere
def getState():
with open('state.tmp', 'r') as f:
return f.read().replace('\n', '')