-
Notifications
You must be signed in to change notification settings - Fork 5
/
avi.cpp
607 lines (446 loc) · 18.4 KB
/
avi.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
heavily modified and simplfied subset of:
https://github.com/jameszah/ESP32-CAM-Video-Recorder-junior
which is a heavily modifed and simplfied rework of:
https://github.com/jameszah/ESP32-CAM-Video-Recorder
James Zahary
Aug 14, 2022
jameszah/CameraWebServerRecorder is licensed under the
GNU General Public License v3.0
*/
#include "esp_camera.h"
#include "sensor.h"
#include <SD_MMC.h>
#define BUFFSIZE 512
uint8_t buf[BUFFSIZE];
#define AVIOFFSET 240 // AVI main header length
int framesize = FRAMESIZE_HD;
int quality = 12;
int avi_length = 900;
int frame_interval = 0;
int speed_up_factor = 1;
camera_fb_t * fb_curr = NULL;
long current_frame_time;
long last_frame_time;
int start_record = 0;
int file_number = 0;
int freespace = 0;
#define fbs 2 // how many kb of static ram for psram -> sram buffer for sd write
uint8_t framebuffer_static[fbs * 1024 + 20];
File avifile;
File idxfile;
char avi_file_name[100];
static int i = 0;
uint16_t frame_cnt = 0;
uint16_t remnant = 0;
uint32_t length = 0;
uint32_t startms;
uint32_t elapsedms;
uint32_t uVideoLen = 0;
int bad_jpg = 0;
int extend_jpg = 0;
int normal_jpg = 0;
#define blinking 0
TaskHandle_t the_camera_loop_task;
long avi_start_time = 0;
long avi_end_time = 0;
unsigned long movi_size = 0;
unsigned long jpeg_size = 0;
unsigned long idx_offset = 0;
uint8_t zero_buf[4] = {0x00, 0x00, 0x00, 0x00};
uint8_t dc_buf[4] = {0x30, 0x30, 0x64, 0x63}; // "00dc"
uint8_t dc_and_zero_buf[8] = {0x30, 0x30, 0x64, 0x63, 0x00, 0x00, 0x00, 0x00};
uint8_t avi1_buf[4] = {0x41, 0x56, 0x49, 0x31}; // "AVI1"
uint8_t idx1_buf[4] = {0x69, 0x64, 0x78, 0x31}; // "idx1"
struct frameSizeStruct {
uint8_t frameWidth[2];
uint8_t frameHeight[2];
};
// data structure from here https://github.com/s60sc/ESP32-CAM_MJPEG2SD/blob/master/avi.cpp, extended for ov5640
static const frameSizeStruct frameSizeData[] = {
{{0x60, 0x00}, {0x60, 0x00}}, // FRAMESIZE_96X96, // 96x96
{{0xA0, 0x00}, {0x78, 0x00}}, // FRAMESIZE_QQVGA, // 160x120
{{0xB0, 0x00}, {0x90, 0x00}}, // FRAMESIZE_QCIF, // 176x144
{{0xF0, 0x00}, {0xB0, 0x00}}, // FRAMESIZE_HQVGA, // 240x176
{{0xF0, 0x00}, {0xF0, 0x00}}, // FRAMESIZE_240X240, // 240x240 4
{{0x40, 0x01}, {0xF0, 0x00}}, // FRAMESIZE_QVGA, // 320x240 5
{{0x90, 0x01}, {0x28, 0x01}}, // FRAMESIZE_CIF, // 400x296 6
{{0xE0, 0x01}, {0x40, 0x01}}, // FRAMESIZE_HVGA, // 480x320 7
{{0x80, 0x02}, {0xE0, 0x01}}, // FRAMESIZE_VGA, // 640x480 8
// 38,400 61,440 153,600
{{0x20, 0x03}, {0x58, 0x02}}, // FRAMESIZE_SVGA, // 800x600 9
{{0x00, 0x04}, {0x00, 0x03}}, // FRAMESIZE_XGA, // 1024x768 10
{{0x00, 0x05}, {0xD0, 0x02}}, // FRAMESIZE_HD, // 1280x720 11
{{0x00, 0x05}, {0x00, 0x04}}, // FRAMESIZE_SXGA, // 1280x1024 12
{{0x40, 0x06}, {0xB0, 0x04}}, // FRAMESIZE_UXGA, // 1600x1200 13
// 3MP Sensors
{{0x80, 0x07}, {0x38, 0x04}}, // FRAMESIZE_FHD, // 1920x1080 14
{{0xD0, 0x02}, {0x00, 0x05}}, // FRAMESIZE_P_HD, // 720x1280 15
{{0x60, 0x03}, {0x00, 0x06}}, // FRAMESIZE_P_3MP, // 864x1536 16
{{0x00, 0x08}, {0x00, 0x06}}, // FRAMESIZE_QXGA, // 2048x1536 17
// 5MP Sensors
{{0x00, 0x0A}, {0xA0, 0x05}}, // FRAMESIZE_QHD, // 2560x1440 18
{{0x00, 0x0A}, {0x40, 0x06}}, // FRAMESIZE_WQXGA, // 2560x1600 19
{{0x38, 0x04}, {0x80, 0x07}}, // FRAMESIZE_P_FHD, // 1080x1920 20
{{0x00, 0x0A}, {0x80, 0x07}} // FRAMESIZE_QSXGA, // 2560x1920 21
};
const int avi_header[AVIOFFSET] PROGMEM = {
0x52, 0x49, 0x46, 0x46, 0xD8, 0x01, 0x0E, 0x00, 0x41, 0x56, 0x49, 0x20, 0x4C, 0x49, 0x53, 0x54,
0xD0, 0x00, 0x00, 0x00, 0x68, 0x64, 0x72, 0x6C, 0x61, 0x76, 0x69, 0x68, 0x38, 0x00, 0x00, 0x00,
0xA0, 0x86, 0x01, 0x00, 0x80, 0x66, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x02, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4C, 0x49, 0x53, 0x54, 0x84, 0x00, 0x00, 0x00,
0x73, 0x74, 0x72, 0x6C, 0x73, 0x74, 0x72, 0x68, 0x30, 0x00, 0x00, 0x00, 0x76, 0x69, 0x64, 0x73,
0x4D, 0x4A, 0x50, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x74, 0x72, 0x66,
0x28, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00,
0x01, 0x00, 0x18, 0x00, 0x4D, 0x4A, 0x50, 0x47, 0x00, 0x84, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x4E, 0x46, 0x4F,
0x10, 0x00, 0x00, 0x00, 0x6A, 0x61, 0x6D, 0x65, 0x73, 0x7A, 0x61, 0x68, 0x61, 0x72, 0x79, 0x20,
0x76, 0x35, 0x35, 0x20, 0x4C, 0x49, 0x53, 0x54, 0x00, 0x01, 0x0E, 0x00, 0x6D, 0x6F, 0x76, 0x69,
};
//
// Writes an uint32_t in Big Endian at current file position
//
static void inline print_quartet(unsigned long i, File fd) {
uint8_t y[4];
y[0] = i % 0x100;
y[1] = (i >> 8) % 0x100;
y[2] = (i >> 16) % 0x100;
y[3] = (i >> 24) % 0x100;
size_t i1_err = fd.write(y , 4);
}
//
// Writes 2 uint32_t in Big Endian at current file position
//
static void inline print_2quartet(unsigned long i, unsigned long j, File fd) {
uint8_t y[8];
y[0] = i % 0x100;
y[1] = (i >> 8) % 0x100;
y[2] = (i >> 16) % 0x100;
y[3] = (i >> 24) % 0x100;
y[4] = j % 0x100;
y[5] = (j >> 8) % 0x100;
y[6] = (j >> 16) % 0x100;
y[7] = (j >> 24) % 0x100;
size_t i1_err = fd.write(y , 8);
}
esp_err_t init_sdcard() {
int succ = SD_MMC.begin("/sdcard", true);
if (succ) {
pinMode(4, OUTPUT); // Blinding Disk-Avtive Light
digitalWrite(4, LOW); // turn off
uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);
} else {
Serial.printf("Failed to mount SD card VFAT filesystem. \n");
Serial.println("Do you have an SD Card installed?");
Serial.println("Check pin 12 and 13, not grounded, or grounded with 10k resistors!\n\n");
}
return ESP_OK;
}
camera_fb_t * get_good_jpeg() {
camera_fb_t * fb;
int failures = 0;
do {
int fblen = 0;
int foundffd9 = 0;
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera Capture Failed");
failures++;
} else {
fblen = fb->len;
for (int j = 1; j <= 1025; j++) {
if (fb->buf[fblen - j] != 0xD9) {
// no d9, try next for
} else { //Serial.println("Found a D9");
if (fb->buf[fblen - j - 1] == 0xFF ) { //Serial.print("Found the FFD9, junk is "); Serial.println(j);
if (j == 1) {
//normal_jpg++;
} else {
//extend_jpg++;
}
foundffd9 = 1;
break;
}
}
}
if (!foundffd9) {
//bad_jpg++;
Serial.printf("Bad jpeg, Frame %d, Len = %d \n", frame_cnt, fblen);
esp_camera_fb_return(fb);
failures++;
} else {
break;
// count up the useless bytes
}
}
} while (failures < 10); // normally leave the loop with a break()
// if we get 10 bad frames in a row, then quality parameters are too high - set them lower (+5), and start new movie
if (failures == 10) {
Serial.printf("10 failures");
sensor_t * ss = esp_camera_sensor_get();
int qual = ss->status.quality ;
ss->set_quality(ss, qual + 5);
quality = qual + 5;
Serial.printf("\n\nDecreasing quality due to frame failures %d -> %d\n\n", qual, qual + 5);
delay(1000);
start_record = 0;
}
return fb;
}
static void start_avi() {
struct tm timeinfo;
time_t now;
Serial.println("Starting an avi ");
time(&now);
localtime_r(&now, &timeinfo);
char strftime_buf[64];
strftime(strftime_buf, sizeof(strftime_buf), "%F_%H.%M.%S", &timeinfo);
sprintf(avi_file_name, "/%s.avi", strftime_buf);
avifile = SD_MMC.open(avi_file_name, "w");
idxfile = SD_MMC.open("/idx.tmp", "w");
if (avifile) {
Serial.printf("File open: %s\n", avi_file_name);
} else {
Serial.println("Could not open file");
}
if (idxfile) {
//Serial.printf("File open: %s\n", "//idx.tmp");
} else {
Serial.println("Could not open file /idx.tmp");
}
for ( i = 0; i < AVIOFFSET; i++) {
char ch = pgm_read_byte(&avi_header[i]);
buf[i] = ch;
}
memcpy(buf + 0x40, frameSizeData[framesize].frameWidth, 2);
memcpy(buf + 0xA8, frameSizeData[framesize].frameWidth, 2);
memcpy(buf + 0x44, frameSizeData[framesize].frameHeight, 2);
memcpy(buf + 0xAC, frameSizeData[framesize].frameHeight, 2);
size_t err = avifile.write(buf, AVIOFFSET);
uint8_t ex_fps = 1;
if (frame_interval == 0) {
if (framesize >= 11) {
ex_fps = 12.5 * speed_up_factor ;
} else {
ex_fps = 25.0 * speed_up_factor;
}
} else {
ex_fps = round(1000.0 / frame_interval * speed_up_factor);
}
avifile.seek( 0x84 , SeekSet);
print_quartet((int)ex_fps, avifile);
avifile.seek( AVIOFFSET, SeekSet);
Serial.print(F("\nRecording "));
Serial.print(avi_length);
Serial.println(" seconds.");
startms = millis();
jpeg_size = 0;
movi_size = 0;
uVideoLen = 0;
idx_offset = 4;
avifile.flush();
} // end of start avi
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// another_save_avi saves another frame to the avi file, uodates index
// -- pass in a fb pointer to the frame to add
//
static void another_save_avi(camera_fb_t * fb ) {
int fblen;
fblen = fb->len;
int fb_block_length;
uint8_t* fb_block_start;
jpeg_size = fblen;
remnant = (4 - (jpeg_size & 0x00000003)) & 0x00000003;
long bw = millis();
long frame_write_start = millis();
framebuffer_static[0] = 0x30; // "00dc"
framebuffer_static[1] = 0x30;
framebuffer_static[2] = 0x64;
framebuffer_static[3] = 0x63;
int jpeg_size_rem = jpeg_size + remnant;
framebuffer_static[4] = jpeg_size_rem % 0x100;
framebuffer_static[5] = (jpeg_size_rem >> 8) % 0x100;
framebuffer_static[6] = (jpeg_size_rem >> 16) % 0x100;
framebuffer_static[7] = (jpeg_size_rem >> 24) % 0x100;
fb_block_start = fb->buf;
if (fblen > fbs * 1024 - 8 ) { // fbs is the size of frame buffer static
fb_block_length = fbs * 1024;
fblen = fblen - (fbs * 1024 - 8);
memcpy(framebuffer_static + 8, fb_block_start, fb_block_length - 8);
fb_block_start = fb_block_start + fb_block_length - 8;
} else {
fb_block_length = fblen + 8 + remnant;
memcpy(framebuffer_static + 8, fb_block_start, fblen);
fblen = 0;
}
size_t err = avifile.write(framebuffer_static, fb_block_length);
if (err != fb_block_length) {
Serial.print("Error on avi write: err = "); Serial.print(err);
Serial.print(" len = "); Serial.println(fb_block_length);
}
while (fblen > 0) {
if (fblen > fbs * 1024) {
fb_block_length = fbs * 1024;
fblen = fblen - fb_block_length;
} else {
fb_block_length = fblen + remnant;
fblen = 0;
}
memcpy(framebuffer_static, fb_block_start, fb_block_length);
size_t err = avifile.write(framebuffer_static, fb_block_length);
if (err != fb_block_length) {
Serial.print("Error on avi write: err = "); Serial.print(err);
Serial.print(" len = "); Serial.println(fb_block_length);
}
fb_block_start = fb_block_start + fb_block_length;
delay(0);
}
movi_size += jpeg_size;
uVideoLen += jpeg_size;
print_2quartet(idx_offset, jpeg_size, idxfile);
idx_offset = idx_offset + jpeg_size + remnant + 8;
movi_size = movi_size + remnant;
avifile.flush();
} // end of another_pic_avi
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// end_avi writes the index, and closes the files
//
static void end_avi() {
unsigned long current_end = avifile.position();
Serial.println("End of avi - closing the files");
if (frame_cnt < 5 ) {
Serial.println("Recording screwed up, less than 5 frames, forget index\n");
idxfile.close();
avifile.close();
int xx = SD_MMC.remove("/idx.tmp");
int yy = SD_MMC.remove(avi_file_name);
} else {
elapsedms = millis() - startms;
float fRealFPS = (1000.0f * (float)frame_cnt) / ((float)elapsedms) * speed_up_factor;
float fmicroseconds_per_frame = 1000000.0f / fRealFPS;
uint8_t iAttainedFPS = round(fRealFPS) ;
uint32_t us_per_frame = round(fmicroseconds_per_frame);
//Modify the MJPEG header from the beginning of the file, overwriting various placeholders
avifile.seek( 4 , SeekSet);
print_quartet(movi_size + 240 + 16 * frame_cnt + 8 * frame_cnt, avifile);
avifile.seek( 0x20 , SeekSet);
print_quartet(us_per_frame, avifile);
unsigned long max_bytes_per_sec = (1.0f * movi_size * iAttainedFPS) / frame_cnt;
avifile.seek( 0x24 , SeekSet);
print_quartet(max_bytes_per_sec, avifile);
avifile.seek( 0x30 , SeekSet);
print_quartet(frame_cnt, avifile);
avifile.seek( 0x8c , SeekSet);
print_quartet(frame_cnt, avifile);
avifile.seek( 0x84 , SeekSet);
print_quartet((int)iAttainedFPS, avifile);
avifile.seek( 0xe8 , SeekSet);
print_quartet(movi_size + frame_cnt * 8 + 4, avifile);
Serial.println(F("\n*** Video recorded and saved ***\n"));
Serial.printf("Recorded %5d frames in %5d seconds\n", frame_cnt, elapsedms / 1000);
Serial.printf("File size is %u bytes\n", movi_size + 12 * frame_cnt + 4);
Serial.printf("Adjusted FPS is %5.2f\n", fRealFPS);
Serial.printf("Max data rate is %lu bytes/s\n", max_bytes_per_sec);
Serial.printf("Frame duration is %d us\n", us_per_frame);
Serial.printf("Average frame length is %d bytes\n", uVideoLen / frame_cnt);
Serial.printf("Writng the index, %d frames\n", frame_cnt);
avifile.seek( current_end , SeekSet);
idxfile.close();
size_t i1_err = avifile.write(idx1_buf, 4);
print_quartet(frame_cnt * 16, avifile);
idxfile = SD_MMC.open("/idx.tmp", "r");
if (idxfile) {
//Serial.printf("File open: %s\n", "//idx.tmp");
} else {
Serial.println("Could not open index file");
}
char * AteBytes;
AteBytes = (char*) malloc (8);
for (int i = 0; i < frame_cnt; i++) {
size_t res = idxfile.readBytes( AteBytes, 8);
size_t i1_err = avifile.write(dc_buf, 4);
size_t i2_err = avifile.write(zero_buf, 4);
size_t i3_err = avifile.write((uint8_t *)AteBytes, 8);
}
free(AteBytes);
idxfile.close();
avifile.close();
int xx = SD_MMC.remove("/idx.tmp");
}
Serial.println("---");
}
void the_camera_loop (void* pvParameter) {
Serial.print("the camera loop, core "); Serial.print(xPortGetCoreID());
Serial.print(", priority = "); Serial.println(uxTaskPriorityGet(NULL));
init_sdcard();
frame_cnt = 0;
speed_up_factor = 1;
avi_length = 900;
frame_interval = 0;
Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));
uint64_t total = SD_MMC.totalBytes();
uint64_t used = SD_MMC.usedBytes();
freespace = (int)((total - used) / (1024 * 1024));
start_record = 0;
delay(1000);
while (1) {
// if (frame_cnt == 0 && start_record == 0) // do nothing
// if (frame_cnt == 0 && start_record == 1) // start a movie
// if (frame_cnt > 0 && start_record == 0) // stop the movie
// if (frame_cnt > 0 && start_record != 0) // another frame
/////////////////// NOTHING TO DO //////////////////
if (frame_cnt == 0 && start_record == 0) {
//Serial.println("Do nothing");
delay(500);
/////////////////// START A MOVIE //////////////////
} else if (frame_cnt == 0 && start_record == 1) {
//Serial.println("Ready to start");
avi_start_time = millis();
Serial.printf("\nStart the avi ... at %d\n", avi_start_time);
Serial.printf("Framesize %d, length %d seconds\n\n", framesize, avi_length);
frame_cnt++;
fb_curr = get_good_jpeg();
start_avi();
another_save_avi( fb_curr);
esp_camera_fb_return(fb_curr);
if (blinking) digitalWrite(33, frame_cnt % 2); // blink
/////////////////// END THE MOVIE //////////////////
} else if ( (frame_cnt > 0 && start_record == 0) || millis() > (avi_start_time + avi_length * 1000)) { // end the avi
Serial.println("End the Avi");
end_avi(); // end the movie
if (blinking) digitalWrite(33, HIGH); // light off
delay(50);
avi_end_time = millis();
float fps = 1.0 * frame_cnt / ((avi_end_time - avi_start_time) / 1000) ;
Serial.printf("End the avi at %d. It was %d frames, %d ms at %.2f fps...\n", millis(), frame_cnt, avi_end_time, avi_end_time - avi_start_time, fps);
Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));
uint64_t total = SD_MMC.totalBytes();
uint64_t used = SD_MMC.usedBytes();
freespace = (int)((total - used) / (1024 * 1024));
frame_cnt = 0; // start recording again on the next loop
/////////////////// ANOTHER FRAME //////////////////
} else if (frame_cnt > 0 && start_record != 0) { // another frame of the avi
//Serial.println("Another frame");
current_frame_time = millis();
if (current_frame_time - last_frame_time < frame_interval) {
delay(frame_interval - (current_frame_time - last_frame_time)); // delay for timelapse
}
last_frame_time = millis();
frame_cnt++;
fb_curr = get_good_jpeg();
another_save_avi( fb_curr);
esp_camera_fb_return(fb_curr);
if (blinking) digitalWrite(33, frame_cnt % 2);
}
yield(); //delay(5);
}
}