Why do I receive an "Unrecognized field name 'SampleRate'" error when using "writeall" with MP4 files in R2021b?

I am attempting to use the "writeall" function with the "OutputFormat" as MP4. I am receiving the following error:
Error using matlab.io.datastore.FileDatastore/writeall (line 482)
Unrecognized field name "SampleRate".
My code is as follows:
fds = fileDatastore('myvideo.mp4','ReadFcn',@readfcn,'FileExtensions','.mp4');
location = 's3://mybucket/output/';
writeall(fds,location,'OutputFormat','mp4')
function data = readfcn(file)
data = importdata(file);
end
What is causing this error?

 Accepted Answer

The "writeall" function requires the audio sampling rate in order to write with the MP4 output format. You can specify the "SampleRate" field using a custom write function:
 
fds = fileDatastore('myvideo.mp4','ReadFcn',@readfcn,'FileExtensions','.mp4');
location = 's3://mybucket/output/';
writeall(fds,location,'WriteFcn',@writefcn)
function data = readfcn(file)
data = audioread(file);
end
function writefcn(data,writeInfo,outputType)
  sampleRate = 44100;
audiowrite(writeInfo.SuggestedOutputName,data,sampleRate);
end

More Answers (0)

Products

Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!