asammdf icon indicating copy to clipboard operation
asammdf copied to clipboard

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

Open NicolasHaeffner opened this issue 5 years ago • 3 comments

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)

NicolasHaeffner avatar Feb 28 '20 09:02 NicolasHaeffner

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 ( signal_new.samples ) are actually the frame indexes. If not you need to update the samples as well according to the new video content.

danielhrisca avatar Mar 09 '20 07:03 danielhrisca

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)

NicolasHaeffner avatar Mar 10 '20 10:03 NicolasHaeffner

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")

HiLcc avatar Jul 16 '21 02:07 HiLcc