-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplexing.py
111 lines (94 loc) · 3.85 KB
/
multiplexing.py
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
import subprocess
import threading
import os
import audio_video_recorder
import audio_recorder
import video_recorder
from datetime import datetime
import time
# REENCODED_FILENAME = "temp_video2.mp4"
REENCODED_FILENAME = "temp_video2.avi"
# FOLDER = "~Videos/fou/"
# FOLDER = "home/hellema/fou/"
FOLDER = "."
def aggregate_audio_video(video_thread, filepath):
"""Audio and Video has to be aggregated into a file"""
frame_counts = video_thread.frame_counts
elapsed_time = datetime.now() - video_thread.start_time
recorded_fps = frame_counts / elapsed_time.total_seconds()
video_thread.join(timeout=float(video_thread.duration+1)) # we use timeout so we're sure we close the window
if video_thread.is_alive():
print("Video thread is alive : Timeout")
while threading.active_count() > 1:
time.sleep(1)
if abs(recorded_fps - video_recorder.FPS ) >= 0.01:
print("Re-encoding")
cmd = "ffmpeg -r " + str(recorded_fps) + " -i "+ video_recorder.VIDEO_FILENAME \
+ " -pix_fmt yuv420p -r 6 "+ REENCODED_FILENAME
subprocess.call(cmd, shell=True)
print("Multiplexing")
cmd = "ffmpeg -ac 2 -channel_layout stereo -i "+ audio_recorder.AUDIO_FILENAME \
+" -i "+ REENCODED_FILENAME+" -pix_fmt yuv420p " + filepath
subprocess.call(cmd, shell=True)
else:
print("Normal recording\nMultiplexing")
cmd = "ffmpeg -ac 2 -channel_layout stereo -i "+ audio_recorder.AUDIO_FILENAME \
+" -i "+ video_recorder.VIDEO_FILENAME + " -pix_fmt yuv420p " + filepath
subprocess.call(cmd, shell=True)
def clean_temp_file():
""""Remove temporary files"""
local_path = os.getcwd()
if os.path.exists(str(local_path) + "/"+audio_recorder.AUDIO_FILENAME):
os.remove(str(local_path) + "/"+ audio_recorder.AUDIO_FILENAME)
if os.path.exists(str(local_path) + "/"+ video_recorder.VIDEO_FILENAME):
os.remove(str(local_path) + "/"+video_recorder.VIDEO_FILENAME)
if os.path.exists(str(local_path) + "/"+REENCODED_FILENAME):
os.remove(str(local_path) + "/"+REENCODED_FILENAME)
def buildfilename():
"""Build filename by removing the digits at the end and the : separating the time"""
time = datetime.now()
filename = str(time).split(".")[0].replace(":","").replace("-","").replace(" ","_") + ".mp4"
return filename
def buildfilepath(filename):
"""Build filepath of the file to store it in the right folder"""
if filename is None:
filename = buildfilename()
filepath = os.path.join(FOLDER+ filename)
return filepath
def record_video_with_audio(duration):
"""Record a video with audio during the time duration"""
filepath = buildfilename()
print("Recording started")
video_thread = video_recorder.VideoThread(duration)
p_audio, stream = audio_recorder.audio_init()
audio_frames = audio_recorder.audio_recording(stream, duration)
video_thread.start()
time.sleep(float(duration))
video_thread.join(timeout=float(duration+1)) # we use timeout so we're sure we close the window
if video_thread.is_alive():
print("Video thread is alive : Timeout")
audio_recorder.audio_stop(p_audio, stream, audio_frames)
print("Recording finished")
aggregate_audio_video(video_thread,filepath)
print("Video is saved as " + filepath)
clean_temp_file()
# TODO: audio is not captured...
def record_video_with_audio_threads(duration):
"""Record a video with audio during the time duration"""
filename = buildfilename()
# filepath = buildfilepath(filename) #TODO doesn't save in the right folder check user access
filepath = filename
video_thread = video_recorder.VideoThread(duration)
audio_thread = audio_recorder.AudioThread(duration)
audio_thread.start()
video_thread.start()
time.sleep(float(duration))
print("Recording finished")
audio_thread.join(timeout=float(duration+1))
if audio_thread.is_alive():
print("Audio thread is alive : Timeout")
print("Audio recording has finished")
aggregate_audio_video(video_thread,filepath)
print("Video is saved as " + filepath)
clean_temp_file()
return filename