I'm trying to compare data from excel sheet with data obtained and extracted in matlab
5 views (last 30 days)
Show older comments
Shrunali Paygude
on 4 Feb 2019
Commented: Shrunali Paygude
on 7 Feb 2019
I'm reading a video as text file and getting the output. Which is as follows:
""ftypisom isomiso2avc1mp41 free zmdat }e�� �!�N��+�\�~�C/x_D�0�.@�]K�M
�E
�y�5�=�� -K\b'��鉧0��gŪc� �� :�q#�e$͉���!�sv+ݓf�>~w ;"�1�P�:��l�%"
Now i want to extract the important part of that output such as ftyp, isom, free, mdat. For that i'm using strfind command to find its position and also i'll know that if the given string is present or not.
a=strfind(B,'ftyp'); //where B is my above output.
I want to create a sequence of ftyp,isom,free,mdat.... (along with its position obtained in above command )in excel sheet.
I want to compare above obtained sequence with a sequence orignally stored in excel.
eg: if orignal sequence in excel1 is ftyp,isom,free,mdat...
and the sequence obtained by extracting from the video is ftyp,isom,mdat.
I should get that my "free" is missing!
How can i do that using xmlread and xmlwrite?
Thank you in advance.
0 Comments
Accepted Answer
Guillaume
on 4 Feb 2019
How can i do that using xmlread and xmlwrite?
Why do you think that xlmread or xmlwrite are relevant here? Your data is not xml. It can't be manipulated by these functions.
Why are you treating a video as text? It's clearly not text, it's binary data and should be treated as such. It would be bad luck but it's very much possible that the binary data anywhere in the file makes the string ftyp. It would be much simpler if you read the data as the structured binary data that it is (with fread calls), you wouldn't need any strfind that way.
Here are the specification of the file format, and here the MP4 extension. From that you can write a simple parser.
fid = fopen('somefile.mp4', 'r', 'b'); %MP4 always encoded in big-endian
boxes = containers.Map; %to store all the boxes.
while ~feof(fid) %read until end of file
boxsize = fread(fid, 1, 'uint32'); %get size of box
if isempty(boxsize)
break; %we reached the end of file
end
boxtype = fread(fid, [1 4], '*char'); %strictly speaking the type is uint32, but it's readable as 4 characters
if boxsize == 1 %extended size first
boxsize = fread(fid, 1, 'uint64');
boxcontent = fread(fid, [1, boxsize - 16], '*uint8'); %read content of box as uint8. We've already read 16 bytes (size, type, extended size)
elseif boxsize == 0 %box extends to end of file
boxcontent = fread(fid, [1, Inf], '*uint8');
else
boxcontent = fread(fid, [1, boxsize - 8], '*uint8'); %read content of box as uint8. We've already read 16 bytes (size, type, extended size)
end
%you're interested in ftyp, free, and mdat. Let's special case them
if strcmp(boxtype, 'ftyp')
boxcontent = struct('major_brand', char(boxcontent(1:4)), 'minor_version', char(boxcontent(5:8)), 'compatible_brands', char(boxcontent(9:end)));
%note: there's nothing special to do for free and mdat. free shouldn't have any content or is irrelevant. mdat content is bytes anyway
else
boxcontent = struct('data', boxcontent); %convert into structure for easy storage in map
end
%now store the box
if ismember(boxtype, boxes.keys) %already had a box of that type
bx = boxes(boxtype); %get already stored box
bx(end+1) = boxcontent; %append to it
boxes(boxtype) = bx; %and store back
else
boxes(boxtype) = boxcontent; %create new box in map
end
end
fclose(fid)
At the end of that you can easily query
boxes.keys
to see all the boxes in your file. And look at their content if they exist:
boxes('ftyp') %guaranteed to exist
boxes('free')
boxes('mdat')
9 Comments
Guillaume
on 6 Feb 2019
I'm afraid I haven't got the time to implement a fully fledged parser for the mp4 format. You're going to have to dig into the data of the container boxes to get the boxes inside them. The simplest would be to delegate the parsing of the boxes to a recursive function. The parsing code would be more or less the same as I've already written.
Things to watch out for:
- Some boxes are what the ISO standard call full. Their header also include version and flag data. Then the contained boxes.
- Format of the file is big-endian. If you convert raw bytes to other types (such as uint32), don't forget to swapbytes them.
- I've not written any error handling. That's probably necessary.
The parsing is not particularly complicated. Just read the description of the boxes in the standard to know their structure. Don't be tempted to treat the raw data as text. As I said, some of the binary data (such as the video data itself) could inadvertently contain something that looks like text but isn't.
More Answers (0)
See Also
Categories
Find more on MATLAB Report Generator in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!