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
| # pip install pyaudio wave moviepy screen_recorder_sdk
# AUDIO
import pyaudio
import wave
# VIDEO
from screen_recorder_sdk import screen_recorder
# AUDIO
# the file name output you want to record into
filename = "audio.wav"
# set the chunk size of 1024 samples
chunk = 1024
# sample format
FORMAT = pyaudio.paInt16
# mono, change to 2 if you want stereo
channels = 2
# 44100 samples per second
sample_rate = 44100
# initialize PyAudio object
p = pyaudio.PyAudio()
# Avoir la liste des périphériques audio
for i in range(0, p.get_device_count()):
print(i, p.get_device_info_by_index(i)['name'])
# Sélectionner "3 Mixage stéréo (Realtek(R) Audio" pour avoir le son sortant
device_index = int(input('Device index: '))
# open stream object as input & output
stream = p.open(format=FORMAT,
channels=channels,
rate=sample_rate,
input=True,
output=True,
frames_per_buffer=chunk,
input_device_index = device_index)
frames = []
print("Recording...")
recording = True
# VIDEO
params = screen_recorder.RecorderParams ()
screen_recorder.init_resources (params)
screen_recorder.start_video_recording ('video.mp4', 30, 8000000, True)
# AUDIO
while(recording == True):
try:
data = stream.read(chunk)
# if you want to hear your voice while recording
stream.write(data)
frames.append(data)
except KeyboardInterrupt:
print("Ctrl + c")
break
except:
break #generic error processing
# stop and close stream
stream.stop_stream()
stream.close()
# terminate pyaudio object
p.terminate()
#VIDEO
screen_recorder.stop_video_recording ()
screen_recorder.free_resources ()
print("Finished recording.")
# save audio file
# open the file in 'write bytes' mode
wf = wave.open(filename, "wb")
# set the channels
wf.setnchannels(channels)
# set the sample format
wf.setsampwidth(p.get_sample_size(FORMAT))
# set the sample rate
wf.setframerate(sample_rate)
# write the frames as bytes
wf.writeframes(b"".join(frames))
# close the file
wf.close()
# Pour combiner l'audio et la vidéo dans une vidéo
def combine_audio(vidname, audname, outname, fps=30):
import moviepy.editor as mpe
my_clip = mpe.VideoFileClip(vidname)
audio_background = mpe.AudioFileClip(audname)
final_clip = my_clip.set_audio(audio_background)
final_clip.write_videofile(outname,fps=fps)
combine_audio("video.mp4", "audio.wav", "ma_video.mp4") |
Partager