1

I need to record and save a video and its corresponding audio using Python. I am able to individually do that (save audio and then save video), but it does not happen simultaneously.

Of course, I thought of using multiprocessing, but that still did not work

p1 = Process(target=listen_to_speaker())
p1.start()
# needs to run at the same time as
p2 = Process(target=record_save())
p2.start()

Here,

listen_to_speaker() records the audio and saves it in a .wav file (and also stores the text conversion of that audio by using the speech_recognition module).

record_save() uses OpenCV to record a video from the Webcam and stores it in an .avi file

Currently, the audio is recorded and saved, and then the webcam is turned on for recording the video.

I also tried adding:

p1.join()
p2.join()

There was no change in the way things turned out.

My expected result is 2 files, one video and one audio, which correspond to each other.

Anubhav Dinkar
  • 343
  • 4
  • 16

1 Answers1

1

target= needs function's name without () and without arguments. It is called "callback".

p1 = Process(target=listen_to_speaker)

p2 = Process(target=record_save)

If you would have to run functions with arguments then you can assign them to args as tuple

p1 = Process(target=listen_to_speaker, args=(arg1,arg2) )

p2 = Process(target=record_save, args=(arg1,arg2) )

If function would need only one argument then you still would have to use tuple args=(arg1,)


Your current code works similar to this code

result1 = listen_to_speaker()
result2 = record_save()

p1 = Process(target=result1)
p1.start()

p2 = Process(target=result2)
p2.start()

so you run functions in main thread and you start processes with results from functions.

If they return nothing then you have target=None so processes don't even start

furas
  • 134,197
  • 12
  • 106
  • 148
  • I get this error message when I run that code: +[AVCaptureDevice initialize] may have been in progress in another thread when fork() was called. – Anubhav Dinkar Jul 21 '19 at 07:58
  • 1
    it is different problem - maybe second process has to wait till first initializes device or maybe device can't work with two threads at the same time. Create new question on new page, describe new problem and show code in `listen_to_speaker`, `record_save` – furas Jul 21 '19 at 08:08
  • [Done](https://stackoverflow.com/questions/57131732/avcapturedevice-initialize-may-have-been-in-progress-in-another-thread-when-f) – Anubhav Dinkar Jul 21 '19 at 09:02