Automatic Video File Naming

326 views
Skip to first unread message

Thomas Bassett

unread,
Dec 16, 2021, 10:57:07 AM12/16/21
to Bonsai Users
Hey Everyone,

I am in the process of designing a video recording workflow that only writes video when a TTL pulse detected by an attached Arduino. While I have gotten this working, I have run into an issue adding an additional feature involving the automated assignment of file names to the various video recordings. To provide context, I've designed the workflow to record video whenever the TTL reader registers a high value and terminates the recording when the reader registers a low value. The plan is to have multiple recordings occur throughout the duration of a singular workflow run. What I was hoping to do is use the 'Externalize Property' feature of VideoWriter to deliver unique video filenames to the writer during the workflow run to create multiple video files, each lasting the duration of the TTL high input. Additionally, I was hoping to read in names from a CSV reader to provide the unique video filenames. My attempts at this, however, have not worked and I'm not sure if what I want to do is possible with this platform. What's currently happening with my workflow is the first filename from the CSV file is being read in and delivered to the VideoWriter, and all additional triggered recordings are being concatenated to the end of the first video file. This results in a singular, long video file with the name of the first CSV input as opposed to a series of shorter ones utilizing all the CSV input names. I've attached my workflow to this message, and am hoping that someone in this group can provide some feedback and/or direction.

Best,

Tom
Video Recording Workflow.bonsai.layout
Video Recording Workflow.bonsai

brunocruz

unread,
Dec 16, 2021, 2:44:32 PM12/16/21
to Bonsai Users
Heya.

Here's a quick stab at it.
First thing, how to save clips? There are a few ways to do this, the one I find the easiest to understand is using selectMany.
Let's first create a subject (publishSubject) for your camera stream and I will simulate an Arduino using keyPresses. Essentially, clicking A and S will output a true or a false, respectively (FakeArduino -> TriggerState).

Now, how do we use this information to save clips?
We listen to TriggerState and look for a rising edge (false->true transition). We do this by simply looking for true values with a condition. For each rising edge, we trigger the "SelectMany (saveclip)". Inside we immediately subscribe to the camera stream (SubscribeWhen). And save the data until a falling edge (true-> false transition, Condition that only lets "false" pass) and use it to close the branch using "TakeUntil". This should take care of saving clips. How do we name them?

We load a full csv (CsvReader), convert it to a list that we can index, and send it to a behaviorSubject to use later. We can index these lists using the "index" node. To keep track of this index, I will add a second variable "currentFileIndex". All there is left to do is to grab an element from this row on each rising edge. To do that, we will take the current index (withLatestFrom * currentFileIndex) and index the list with it. (also we need to increment currentFileIndex by one. Add(1) -> MulticastSubject). Finally, we use the indexed string to name the video file. Make sure the number of trials is, at most, the number of rows in your csvFile. Otherwise, the workflow will crash. I did some quick tests and it seems to be working as intended but let me know if something does not behave as expected or if I was not clear in any of the steps.

Cheers,
Bruno
Video Recording Workflow.bonsai

Thomas Bassett

unread,
Dec 17, 2021, 10:34:31 AM12/17/21
to Bonsai Users
This script seems to be working well, thank you so much for the assistance. I'm attaching my copy of my script with the functional Arduino code incase anyone else finds it useful.

Tom

TSE Chamber Recording Software.bonsai.layout
TSE Chamber Recording Software.bonsai
Reply all
Reply to author
Forward
0 new messages