esp32-cam-micropython-2022 icon indicating copy to clipboard operation
esp32-cam-micropython-2022 copied to clipboard

How to record video

Open shaguahehehe opened this issue 1 year ago • 16 comments

How to record video

shaguahehehe avatar May 28 '23 13:05 shaguahehehe

The image is in jpg format. We have no support the output formats avi or mp4. You can capture a jpg image in a loop and save it to a file. See the following example.

import camera, machine, uos, gc
from time import sleep

# init camera
camera.init()

# set preffered camera setting
camera.framesize(10)     # frame size 800X600 (1.33 espect ratio)
camera.contrast(2)       # increase contrast
camera.speffect(2)       # jpeg grayscale
img=camera.capture()     # throw away 1.st frame

# jpg file storage
sd=machine.SDCard(slot=1)
uos.mount(sd, "/sd")

for i in range(100): # 100 frames video
    img=camera.capture()
    if len(img) > 0:
       print('Frame:', i)
       fn = '/sd/pic-%03d.jpg'%i
       f = open(fn, 'wb')
       f.write(img)
       f.close()
       del f
    del img
#    sleep(60)
    gc.collect()

uos.listdir("/sd")
uos.umount("/sd")

Now that you have a large number of jpg files, you can convert them to videos. You can use ffmpeg on a Linux computer.

$ ffmpeg -framerate 10 -i /media/sharil/0E0B-1721/pic-%03d.jpg video.mp4

or

$ ffmpeg -framerate 10 -i /media/sharil/0E0B-1721/pic-%03d.jpg video.avi

A low-quality micro-SD card is one item that can cause problems and frustration. If your SD card fails, your script may hang during the writing process. Don't go for the cheapest card you can find on the internet, because that will only cause problems.

shariltumin avatar May 29 '23 11:05 shariltumin

I tried to use Bluetooth to send a instruction to control the camera, but once the Bluetooth emitted the video, it was not closed when the Bluetooth sending the stop recording video was not closed. How can I solve it?

shaguahehehe avatar May 30 '23 15:05 shaguahehehe

I'm not sure how to respond to your question. I'm not sure what you're trying to accomplish. Some specifics and script examples may be useful.

shariltumin avatar May 30 '23 17:05 shariltumin

I tried to use Bluetooth to send a instruction to control the camera, but once the Bluetooth emitted the video, it was not closed when the Bluetooth sending the stop recording video was not closed. How can I solve it?

ble = ESP32_BLE("ESP32BLE") count = 1 flag = False video_path = '' video_file = None

    while True:
        if BLE_MSG == 'open':        
            video_path = '/sd/video' + str(count) + '.avi'
            video_file = open(video_path, 'wb')
            print("开始录制")
            flag = True
            while flag:
                for i in range(100):
                    img = camera.capture()
                    video_file.write(img)                    
                if BLE_MSG == 'close':
                    flag = False
                    print("关闭录制")
                    BLE_MSG = ""          
                    video_file.close()
                    camera.deinit()
                    print('Video saved to', video_path)
            count += 1          
          
        sleep_ms(10)

shaguahehehe avatar May 31 '23 03:05 shaguahehehe

I'm not familiar with the specifics of the ESP32_BLE module. To begin, I'm not sure where in your script the BLE_MSG variable gets its value; let's assume it was set by BLE_MSG = ble.get_message(). Let's simplify your script and see if you ever get BLE_MSG == 'close'.

ble = ESP32_BLE("ESP32BLE")
while 1:
  BLE_MSG = ble.get_message()
  if BLE_MSG == 'open':
     print('OPEN')
  elif BLE_MSG == 'close':
     print('CLOSE')
  else:
     if BLE_MSG:
        print('Unknown cmd:', BLE_MSG)
  sleep_ms(10)

Perhaps ESP32_BLE() is not functioning as expected. You should seek assistance from those who created the module.

shariltumin avatar May 31 '23 08:05 shariltumin

Bluetooth can receive the Open instructions that will enter the circular video, unless the withdrawal of the cycle can receive the next information sent by Bluetooth, is there any way to receive Bluetooth information asynchronously

shaguahehehe avatar May 31 '23 09:05 shaguahehehe

What output you get when you run the simple script above? Try to send different strings from the BLE client.

shariltumin avatar May 31 '23 11:05 shariltumin

Simple examples are completely possible, but what I want to solve is how to allow Bluetooth to receive data asynchronously

shaguahehehe avatar May 31 '23 12:05 shaguahehehe

The BLE device receives data whenever a client, such as a mobile phone application, sends data to it. You can have one thread just pooling for BLE data input and other threads for other processes.

There are several ways to do this. 1) _thread, 2) aioble (using asyncio), and 3) workers. For aioble, you need asyncio. However, the firmware does not support asyncio. You can have a look at aioble here.

So with the current firmware you have 2 alternatives: 1) _thread, and 2) workers. You can have a look at workers here. I find asyncio and aioble difficult to use and am always looking for easier solutions. I found an "esp32_ble.py" and with a few modifications I managed to read data sent by a serial bluetooth terminal from an android phone.

Of course you are free to use whatever solution you like for your project.

shariltumin avatar May 31 '23 13:05 shariltumin

I want to see eSP32_ble.py

shaguahehehe avatar May 31 '23 15:05 shaguahehehe

Bluetooth receives the information to turn on the camera command, will continue to loop recording, if not exit the loop, then Bluetooth will not receive the next command

shaguahehehe avatar May 31 '23 15:05 shaguahehehe

The esp32_ble.py file can be found here

shariltumin avatar May 31 '23 20:05 shariltumin

The same problem I want to solve with your script is that when Esp32 receives a 'start' it will run and can't always send a 'stop' to stop it

shaguahehehe avatar Jun 01 '23 03:06 shaguahehehe

I'm running out of ideas on what to do to fix this problem. It could be a hardware problem. It could be a problem with the SD card. Please run the test script test_sdcard.py to verify that the SD card is OK.

shariltumin avatar Jun 01 '23 09:06 shariltumin

Is there a firmware that supports Bluetooth and camera at the same time?

shaguahehehe avatar Jun 06 '23 14:06 shaguahehehe

If you are referring to Bluetooth Classic, you can find the firmware for it here.

For more information about the Bluetooth Classic module, click here.

shariltumin avatar Jun 07 '23 12:06 shariltumin