how to record a video from a webcam, with sound, from within python? /u/avsfjan Python Education
how to record a video from a webcam, with sound, from within python? /u/avsfjan Python Education
I am writing a python program for windows in which I want the user be able to quickly take a video from within the python app using the internal camera and microphone and then save the resulting video as an mp4, or similar.
The software needs to run on windows (specifically a surface tablet).
Obviously i googled first and found many, many tutorials on how to record video, but most of them without sound. Also all of them had the problem that the resulting video was badly synched and was timewarping around.
So far I tried CV2 and ffmpeg.
The latter via commandline and a separate ffmpeg.exe binary. This works in general, but has the same problem as all the CV2 solutions: audio and video are hardly in sync and the video speed is janky all over the video.
is there a better solution? Is this really this hard, or am i missing something here?
So far my code is this: (selecting cameras[1] and audio[0] is only specific to my test machine right now, this would later be dynamic…)
import subprocess import re import time list_devices = ['../ffmpeg.exe', '-list_devices', 'true', '-f', 'dshow', '-i', 'dummy'] result = subprocess.run( list_devices, # Arguments capture_output = True, # Python >= 3.7 only text = True # Python >= 3.7 only ) cameras = re.findall(""(.*)" (video)", result.stderr) audios = re.findall(""(.*)" (audio)", result.stderr) cam = cameras[1] audio = audios[0] record = ['../ffmpeg.exe', '-f', 'dshow', '-i', f"video={cam}:audio={audio}", '-vf', 'format=yuv420p', 'output.mp4'] recorder = subprocess.Popen(record, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, stdin=subprocess.PIPE) time.sleep(30) recorder.stdin.write('q') # Simulate user pressing q key recorder.communicate() recorder.wait() print('done')
submitted by /u/avsfjan
[link] [comments]
r/learnpython I am writing a python program for windows in which I want the user be able to quickly take a video from within the python app using the internal camera and microphone and then save the resulting video as an mp4, or similar. The software needs to run on windows (specifically a surface tablet). Obviously i googled first and found many, many tutorials on how to record video, but most of them without sound. Also all of them had the problem that the resulting video was badly synched and was timewarping around. So far I tried CV2 and ffmpeg. The latter via commandline and a separate ffmpeg.exe binary. This works in general, but has the same problem as all the CV2 solutions: audio and video are hardly in sync and the video speed is janky all over the video. is there a better solution? Is this really this hard, or am i missing something here? So far my code is this: (selecting cameras[1] and audio[0] is only specific to my test machine right now, this would later be dynamic…) import subprocess import re import time list_devices = [‘../ffmpeg.exe’, ‘-list_devices’, ‘true’, ‘-f’, ‘dshow’, ‘-i’, ‘dummy’] result = subprocess.run( list_devices, # Arguments capture_output = True, # Python >= 3.7 only text = True # Python >= 3.7 only ) cameras = re.findall(“”(.*)” (video)”, result.stderr) audios = re.findall(“”(.*)” (audio)”, result.stderr) cam = cameras[1] audio = audios[0] record = [‘../ffmpeg.exe’, ‘-f’, ‘dshow’, ‘-i’, f”video={cam}:audio={audio}”, ‘-vf’, ‘format=yuv420p’, ‘output.mp4’] recorder = subprocess.Popen(record, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, stdin=subprocess.PIPE) time.sleep(30) recorder.stdin.write(‘q’) # Simulate user pressing q key recorder.communicate() recorder.wait() print(‘done’) submitted by /u/avsfjan [link] [comments]
I am writing a python program for windows in which I want the user be able to quickly take a video from within the python app using the internal camera and microphone and then save the resulting video as an mp4, or similar.
The software needs to run on windows (specifically a surface tablet).
Obviously i googled first and found many, many tutorials on how to record video, but most of them without sound. Also all of them had the problem that the resulting video was badly synched and was timewarping around.
So far I tried CV2 and ffmpeg.
The latter via commandline and a separate ffmpeg.exe binary. This works in general, but has the same problem as all the CV2 solutions: audio and video are hardly in sync and the video speed is janky all over the video.
is there a better solution? Is this really this hard, or am i missing something here?
So far my code is this: (selecting cameras[1] and audio[0] is only specific to my test machine right now, this would later be dynamic…)
import subprocess import re import time list_devices = ['../ffmpeg.exe', '-list_devices', 'true', '-f', 'dshow', '-i', 'dummy'] result = subprocess.run( list_devices, # Arguments capture_output = True, # Python >= 3.7 only text = True # Python >= 3.7 only ) cameras = re.findall(""(.*)" (video)", result.stderr) audios = re.findall(""(.*)" (audio)", result.stderr) cam = cameras[1] audio = audios[0] record = ['../ffmpeg.exe', '-f', 'dshow', '-i', f"video={cam}:audio={audio}", '-vf', 'format=yuv420p', 'output.mp4'] recorder = subprocess.Popen(record, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, stdin=subprocess.PIPE) time.sleep(30) recorder.stdin.write('q') # Simulate user pressing q key recorder.communicate() recorder.wait() print('done')
submitted by /u/avsfjan
[link] [comments] I am writing a python program for windows in which I want the user be able to quickly take a video from within the python app using the internal camera and microphone and then save the resulting video as an mp4, or similar. The software needs to run on windows (specifically a surface tablet). Obviously i googled first and found many, many tutorials on how to record video, but most of them without sound. Also all of them had the problem that the resulting video was badly synched and was timewarping around. So far I tried CV2 and ffmpeg. The latter via commandline and a separate ffmpeg.exe binary. This works in general, but has the same problem as all the CV2 solutions: audio and video are hardly in sync and the video speed is janky all over the video. is there a better solution? Is this really this hard, or am i missing something here? So far my code is this: (selecting cameras[1] and audio[0] is only specific to my test machine right now, this would later be dynamic…) import subprocess import re import time list_devices = [‘../ffmpeg.exe’, ‘-list_devices’, ‘true’, ‘-f’, ‘dshow’, ‘-i’, ‘dummy’] result = subprocess.run( list_devices, # Arguments capture_output = True, # Python >= 3.7 only text = True # Python >= 3.7 only ) cameras = re.findall(“”(.*)” (video)”, result.stderr) audios = re.findall(“”(.*)” (audio)”, result.stderr) cam = cameras[1] audio = audios[0] record = [‘../ffmpeg.exe’, ‘-f’, ‘dshow’, ‘-i’, f”video={cam}:audio={audio}”, ‘-vf’, ‘format=yuv420p’, ‘output.mp4’] recorder = subprocess.Popen(record, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, stdin=subprocess.PIPE) time.sleep(30) recorder.stdin.write(‘q’) # Simulate user pressing q key recorder.communicate() recorder.wait() print(‘done’) submitted by /u/avsfjan [link] [comments]