shariltumin / esp32-cam-micropython-2022

MicroPython esp32-cam firmware with camera support compiled with esp-idf-4.4.0. Python script files for live streaming
MIT License
174 stars 33 forks source link

How to record video #20

Open shaguahehehe opened 1 year ago

shaguahehehe commented 1 year ago

How to record video

shariltumin commented 1 year ago

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.

shaguahehehe commented 1 year ago

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?

shariltumin commented 1 year ago

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.

shaguahehehe commented 1 year ago

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)
shariltumin commented 1 year ago

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.

shaguahehehe commented 1 year ago

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

shariltumin commented 1 year ago

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

shaguahehehe commented 1 year ago

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

shariltumin commented 1 year ago

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.

shaguahehehe commented 1 year ago

I want to see eSP32_ble.py

shaguahehehe commented 1 year ago

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

shariltumin commented 1 year ago

The esp32_ble.py file can be found here

shaguahehehe commented 1 year ago

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

shariltumin commented 1 year ago

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.

shaguahehehe commented 1 year ago

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

shariltumin commented 1 year ago

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.