SSブログ

SPRESENSE/Arduino でAVI動画を記録 [SPRESENSE]

Qiita投稿2回目です。クリスマスまでいくつ記事がかけるかな??今回は、SPRESENSEやArduinoでAVI動画を記録するライブラリを作ってみました。ここで紹介するサンプルはSPRESENSE用ですが、ライブラリそのものはJPEG画像さえ入手できればArduinoでも使えるように設計しています。


s_github.png YoshinoTaro/AviLibrary_Arduino

Contribute to YoshinoTaro/AviLibrary_Arduino development by creating an account on GitHub. github.com





SPRESENSEやArduinoなどの省電力のボードマイコンはカメラ用のISPが内蔵されていないので動画を撮影することはできません。でも、SPRESENSEのカメラのようにカメラモジュールがJPEG出力している場合は、Motion JPEG(AVI)で動画を簡単に記録できます。


void setup() {
  Serial.begin(115200);
  theCamera.begin();
  while (!theSD.begin()) { Serial.println("insert SD card"); }

  // Setup the still picture parameters of Spresense Camera
  // The image format must be JPEG
  theCamera.setStillPictureImageFormat(
     CAM_IMGSIZE_QUADVGA_H, CAM_IMGSIZE_QUADVGA_V, CAM_IMAGE_PIX_FMT_JPG);

  theSD.remove(filename);
  aviFile = theSD.open(filename, FILE_WRITE);

  // Initialize AVI library
  theAvi.begin(aviFile, CAM_IMGSIZE_QUADVGA_H, CAM_IMGSIZE_QUADVGA_V);
  theAvi.startRecording();
}

void loop() {
  static uint32_t start_time = millis();
  CamImage img = theCamera.takePicture();

  // Add a frame. The image format must be JPEG
  theAvi.addFrame(img.getImgBuff(), img.getImgSize());

  uint32_t duration = millis() - start_time;
  if (duration > recording_time_in_ms) {
    // Stop AVI library
    theAvi.endRecording();
    theAvi.end();
    theCamera.end();
    while (true) {}
  }
}



このライブラリは実時間で記録することもできますが、FPSを指定して再生速度を指定することができます。特にタイムラプスの使用を想定しているので、数分毎に撮影した画像を指定したFPSで再生することができます。

void setup() {
  Serial.begin(115200);
  theCamera.begin();
  while (!theSD.begin()) { Serial.println("insert SD card"); }

  // Setup the still picture parameters of Spresense Camera
  // The image format must be JPEG
  theCamera.setStillPictureImageFormat(
     CAM_IMGSIZE_QUADVGA_H, CAM_IMGSIZE_QUADVGA_V, CAM_IMAGE_PIX_FMT_JPG);

  theSD.remove(filename);
  aviFile = theSD.open(filename, FILE_WRITE);

  // Initialize AVI library
  theAvi.begin(aviFile, CAM_IMGSIZE_QUADVGA_H, CAM_IMGSIZE_QUADVGA_V);
  // Start the timelapse specifying fps
  theAvi.startTimelapse(target_fps);
}

void loop() {
  static uint32_t start_time = millis();
  CamImage img = theCamera.takePicture();
  // Add a timelapse frame. The image format must be JPEG
  theAvi.addTimelapseFrame(img.getImgBuff(), img.getImgSize());

  uint32_t duration = millis() - start_time;
  if (duration > recording_time_in_ms) {
    // Stop AVI library
    theAvi.endTimelapse();
    theAvi.end();
    theCamera.end();
    while (true) {}
  }
}


特にSPRESENSEの場合は、低消費電力で動かすことができるので、数分おきに起動して撮影を繰り返すことでバッテリーでも長時間撮影が可能です。省電力カメラのサンプルも用意しています。夜空の撮影や、旅程の記録、自宅の監視用にぜひぞうぞ。

void setup() {
  // Initialize LowPower Library
  LowPower.begin();
  RTC.begin();

  // get boot cause to check whether this boot is caused by the alarm
  bootcause_e bc = LowPower.bootCause();

  Serial.begin(115200);
  theCamera.begin();
  while (!theSD.begin()) { Serial.println("insert SD card"); }

  // Setup the still picture parameters of Spresense Camera
  // The image format must be JPEG
  theCamera.setStillPictureImageFormat(
     CAM_IMGSIZE_QUADVGA_H, CAM_IMGSIZE_QUADVGA_V, CAM_IMAGE_PIX_FMT_JPG);

  // In the case of power on / reset, the parameters are set to the initial value.
  if (bc != DEEP_RTC && bc != DEEP_OTHERS) {
    theSD.remove(filename);
    rec_frame = 0;
    movi_size = 0;
    file_size = 0; 
  } else {
    EEPROM.get(rec_frame_address, rec_frame);  // memory the recorded frame count
    EEPROM.get(movi_size_address, movi_size);  // memory the size of the video
    EEPROM.get(file_size_address, file_size);  // memory the file size
  }

  aviFile = theSD.open(filename, FILE_WRITE);

  // Initialize AVI library
  theAvi.begin(aviFile, CAM_IMGSIZE_QUADVGA_H, CAM_IMGSIZE_QUADVGA_V);
  // set parameters got from EEPROM
  theAvi.setTotalFrame(rec_frame);
  theAvi.setFileSize(file_size);
  theAvi.setMovieSize(movi_size); 
  // Start the timelapse specifying fps
  theAvi.startTimelapse(target_fps);
}

void loop() {
  CamImage img = theCamera.takePicture();
  // Add a timelapse frame. The image format must be JPEG
  theAvi.addTimelapseFrame(img.getImgBuff(), img.getImgSize());
  // end the recording immediately
  theAvi.endTimelapse();
  theAvi.end();
  theCamera.end();

  // memory the updated parameters
  EEPROM.put(rec_frame_address, theAvi.getTotalFrame());
  EEPROM.put(movi_size_address, theAvi.getMovieSize());
  EEPROM.put(file_size_address, theAvi.getFileSize());

  // finish the task when the total frame is over the specified number
  if (theAvi.getTotalFrame() > recording_total_frames) {
    while (true) {}
  }
  // power off until  the time specified by sleep_time
  LowPower.deepSleep(sleep_time);
}




SONY SPRESENSE メインボード CXD5602PWBMAIN1

SONY SPRESENSE メインボード CXD5602PWBMAIN1

  • 出版社/メーカー: スプレッセンス(Spresense)
  • メディア: Tools & Hardware



SONY SPRESENSE カメラモジュール CXD5602PWBCAM1

SONY SPRESENSE カメラモジュール CXD5602PWBCAM1

  • 出版社/メーカー: スプレッセンス(Spresense)
  • メディア: Tools & Hardware



SONY SPRESENSE 拡張ボード CXD5602PWBEXT1

SONY SPRESENSE 拡張ボード CXD5602PWBEXT1

  • 出版社/メーカー: スプレッセンス(Spresense)
  • メディア: Tools & Hardware



タグ:Spresense
nice!(20)  コメント(0) 
共通テーマ:趣味・カルチャー

nice! 20

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。