Reputation: 11
I have a large quantity (thousands) of video files (.MP4) generated by camera traps.
I want to extract the sounds (as .WAV or any other relevant format for further analysis in an acoustic software such as Kaleidoscope lite), generate metadata following the GUANO standard and integrate these metadata to the generated sound file. I want to extract as much information as possible, but my main interest is in extracting the date of the .MP4 file creation (corresponding to the start of the video).
Given the number of files, I am looking to do this in an automated manner, preferentially in R (but Python can be an option).
There is an R package "guano" to read the metadata of a file containing it, but there is no function to create a GUANO "slot". I am not completely sure I understand well, but it seems the Python version of the library (guano-py) allows for metadata editing through "guano_edit.py".
Here is what I tried in R:
Batch extraction of sounds from video files with ffmpeg and copy metadata to created file running the following line.
system(sprintf("ffmpeg -i ./data/input/my_file.MP4 -map_metadata 0 -acodec copy -y ./data/output/my_file.wav"))`
(the automation part is done following the logic described (Can I use R to trim a video, like I do with ffmpeg?, thanks Chris)
GUANO reading works well in R using the guano
package (https://github.com/riggsd/guano-r) with files already containing GUANO metadata (recorded with Audiomoth).
read.guano('my_file.WAV'
Unfortunately, the R package does not contain editing functions.
I used the package "reticulate" to launch guano-py from R. GUANO reading/connection works in R using the Python functions. The editing of the GUANO object from R works, but I did not succeed in writing the edited metadata contained within the .WAV file. I did this as follow:
library(reticulate)
conda_create(envname = "r-reticulate", conda = "path_tp/Anaconda3/_conda.exe")
use_condaenv(condaenv = "r-reticulate")
guano <- import("guano")
Opening what I would call a "connection"
gfile <- guano$GuanoFile('.data/output/IMG_0051.wav')
Then edit specific fields, for example:
gfile['Note'] <- 'I love GUANO!'
which I can find back when typing gfile['Note']
The point where I am stuck is the writing of updated .WAV which in Python would be written like this (https://github.com/riggsd/guano-py):
# write the updated .WAV file back to disk
gfile.write()
I tried py_run_string("gfile.write()")
and got the following error
AttributeError: module '__main__' has no attribute 'write'
. I explored the approach with py_run_string
, but from my understanding, I would have to redo a large part in Python of what is already working in R, which I am not willing to do given the time already invested and my level of Python coding.
I tried guano$write("gfile")
and received the following error AttributeError: module 'guano' has no attribute 'write'
. I can see write
is defined at line 449 at this link https://github.com/riggsd/guano-py/blob/master/guano.py.
Would someone have an idea of how I can make gfile.write()
work directly in R? Maybe by properly running the function in the R environment or sending gfile
in the Python environment?
Upvotes: 1
Views: 75