danielhrisca / asammdf

Fast Python reader and editor for ASAM MDF / MF4 (Measurement Data Format) files
GNU Lesser General Public License v3.0
632 stars 224 forks source link

How to copy a signal with a video file as attachment and change the video source #316

Closed NicolasHaeffner closed 11 months ago

NicolasHaeffner commented 4 years ago

Python version

('python=3.8.1 | packaged by conda-forge | (default, Jan 29 2020, 14:24:10) ' '[MSC v.1916 64 bit (AMD64)]') 'os=Windows-10-10.0.17763-SP0' 'numpy=1.18.1' 'asammdf=5.18.0'

Code

MDF version

4.00

snippet

#%% imports 
from asammdf import MDF, Signal
from pathlib import Path

# %% get the signals
mdf = MDF('in.MF4')
signal_old = mdf.get('Multimedia_1')

# %% copy the signal
signal_new = signal_old.copy()
signal_new.name = 'Multimedia_2'

# %% attach new avi 
with open('in.avi', 'rb') as f: 
    data = f.read()
    index = mdf.attach(data, file_name='in.avi', compression=False, embedded=False)
    signal_new.attachment = (data, Path('in.avi'))

# %% append the signal
mdf.append(signal_new)

Description

Hello all :)

I have a MDF File (I cannot share unfortunately) that has a signal with a video attachment (not embedded). I want to duplicate the signal and attach a new (modified) video file.

The documentation is quite sparse here. If I attach the new signal to the mdf file, I get a index returned. How to tell the new signal, that the attachment lies there?

Thanks for the help (and the great work in general)

danielhrisca commented 4 years ago

Hello Nicolas,

the attachment should work for the new signal, but you have to make sure that the new video has the same number of frames as the old one, since the actual channel samples ( _signalnew.samples ) are actually the frame indexes. If not you need to update the samples as well according to the new video content.

NicolasHaeffner commented 4 years ago

Hi Daniel, hmm unfortunately I cannot see the signal in canape after the fact. It is indeed the exact same video in my tests, just duplicated (it is to be modified later, but lengthwise it will stay the same, hence the copying of the signal in the first place)

HiLcc commented 3 years ago

Hello Nicolas, Thanks for sharing! After the modification, I can read the video data .

signal_new = signal_old.copy()
signal_new.name = 'Multimedia_2'

mdf.append(signal_new)

with open('in.avi', 'rb') as f: 
    data = f.read()
    index = mdf.attach(data, file_name='in.avi', compression=False, embedded=False)
    # if channel_type = v4c.CHANNEL_TYPE_SYNC
    mdf.groups[-1].channels[-1].data_block_addr = index
    mdf.attachments[0].file_name = Path(str(mdf.attachments[0].file_name).replace("FROM_", "", 1))
mdf.save("test.mf4")