Create a MF4 file from CSV and AVI files
Python version
('python=3.10.8 | packaged by conda-forge | (main, Nov 24 2022, 14:07:00) [MSC ' 'v.1916 64 bit (AMD64)]') 'os=Windows-10-10.0.19044-SP0' 'numpy=1.23.5' 'asammdf=7.2.0'
Code
MDF version
4.00
Code snippet
#%% Imports
import pandas as pd
import logging
from asammdf import MDF
from os import listdir
from os.path import isfile, join
#%% Directories
csv_directory = "in/csvfile.csv"
avi_directory = "in/avifile.avi"
mf4_directory = "out/mf4file.mf4"
#%% Create empty mdf object
mdf = MDF()
#%% Append data from csv file
df_csv = pd.read_csv(csv_directory )
mdf.append(df_csv)
#%% Attach video
with open(avi_directory , 'rb') as f:
data = f.read()
index = mdf.attach(data, file_name='front_camera.avi', compression=False, embedded=False)
mdf.groups[-1].channels[-1].data_block_addr = index
mdf.attachments[0].file_name = Path(str(mdf.attachments[0].file_name).replace("FROM_", "", 1))
#%% Save to mf4
mdf.save(mf4_directory)
Description
Hi all!
I have some vehicle data from CAN Bus written in csv and a synchronized video with matching number of frames. I would like to display both the signals and the associated video frames in a tool, which requires mf4 as input. Therefore, I have setup the code above to convert csv files and avi files into mf4 format by recycling a similar issue #316. The code does not trigger an error and appends the csv flawlessly to the mdf object, but it doesn't work for the avi attachment (neither size nor channel of mf4 output changes - the mf4 only contains signals from csv input). In debug mode I can see that the avi is successfully read to a binary file (data = b'RIFFX\xceM\x01AVI LIST\xec\x11\x00\x00hdrlavih8\x00\x00\x005\x82 ... ).
I'm quite new to this topic and am struggling to find documentation and threads to this kind of task. Any help is highly appreciated!
import pandas as pd
import logging
from asammdf import MDF
from asammdf.blocks.v4constants import CHANNEL_TYPE_SYNC, SYNC_TYPE_TIME
from os import listdir
from os.path import isfile, join
#%% Directories
csv_directory = "in/csvfile.csv"
avi_directory = "in/avifile.avi"
mf4_directory = "out/mf4file.mf4"
#%% Create empty mdf object
mdf = MDF()
#%% Append data from csv file
df_csv = pd.read_csv(csv_directory )
mdf.append(df_csv)
#%% Attach video
with open(avi_directory , 'rb') as f:
data = f.read()
index = mdf.attach(data, file_name='front_camera.avi', compression=False, embedded=True) # embedded attachment
mdf.groups[-1].channels[-1].data_block_addr = index
mdf.groups[-1].channels[-1].sync_type = SYNC_TYPE_TIME
mdf.groups[-1].channels[-1].channel_type = CHANNEL_TYPE_SYNC
mdf.attachments[0].file_name = Path(str(mdf.attachments[0].file_name).replace("FROM_", "", 1))
#%% Save to mf4
mdf.save(mf4_directory)
Hi Daniel,
sorry for the late replay, but many thanks for your answer! Your provided code works as intended and attaches an embedded video file, as you can see in the screenshot below.

Unfortunately, this doesn't get me very far in terms of visualization videos within my tool. It seems that an additional channel is needed for this, which I did not point out in my original post - sorry about that. Is there also the possibility to add something like a multimedia or video channel to the existing signal channels?
Thank you in advance!
Please send a demo file to see how the channels are stored
Sorry, I can not share demo files because I have to follow protection of data privacy. I will attach some screenshots of the mdf file structure, which hopefully shows what you're asking for. As you can see, I'm navigating into the mdf file > groups > channels > channel 000:
This output is generated by your code snippet. I'm happy to share any other screenshots, if you need more information.
@danielhrisca Hi Daniel, does the current asammdf gui supports displaying video or multimedia attachments? Thanks a lot.
@danielhrisca Hi Daniel, does the current asammdf gui supports displaying video or multimedia attachments? Thanks a lot.
Video windows are not implemented
is there a way to convert a pcap file to mf4
@danielhrisca is there a plan to implement video windows at all? It would be really helpful to be able to display synchronised video with MDF data