-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopSideComp_Client.py
More file actions
65 lines (57 loc) · 2.1 KB
/
Copy pathTopSideComp_Client.py
File metadata and controls
65 lines (57 loc) · 2.1 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
# Client script in the client-server connection
# Meant to be on top-side RaspberryPi/"Remote" laptop
'''
ffmpeg -i test.h264 -c:v copy test.mp4
scp pi_server.py pi@169.254.170.31:~
'''
import socket
import subprocess
import time
import os
# host = '169.254.170.31' # ip address of the Raspberry Pi through eth0
host = '192.168.0.48' # ip address of the Raspberry Pi through the wlan0
port = XXXX # using port 8000 for the connection
cwd = os.getcwd() # gets current working directory to save videos into file
# Use the os package to time-stamp the videos stored
date_stamp = time.strftime('%y-%d-%m')
time_stamp = time.strftime('%I-%M')
directory = os.path.join(cwd,date_stamp)
video_h264 = os.path.join(directory,time_stamp+'.h264')
# Uses TCP protocol to recieve live stream from the pi
def TCP():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host,port))
video_file = open(video_h264,'wb')
connection = client_socket.makefile('rb')
try:
# Run mplayer with the following command line
frame_rate = ['-fps','30']
cache = ['-cache','1024']
#cmdline = ['mplayer', '-fps', '30', '-cache', '1024','-']
cmdline = ['mplayer']+frame_rate+cache+['-']
player = subprocess.Popen(cmdline, stdin=subprocess.PIPE)
while True:
# reads 1k of data and writes it into mplayer's stdin
data = connection.read(1024)
if not data:
break
video_file.write(data)
player.stdin.write(data)
finally:
connection.close()
client_socket.close()
player.terminate()
video_file.close()
def convert(video):
try:
video_mp4 = os.path.join(directory,time_stamp+'.mp4')
cmdline = ['ffmpeg', '-i', video_h264, '-c:v', 'copy', video_mp4]
convert = subprocess.call(cmdline)
finally:
os.remove(video)
if __name__ == '__main__':
print('Connecting to Raspberry Pi...')
if not os.path.exists(directory):
os.makedirs(directory)
TCP()
convert(video_h264)