I have a bytes asset that contains an mp4 within it that I have imported into Unity, the only problem being that I don’t know how to revert it back to it’s original mp4 state. I have no experience in Unity but this is the only way I can get this mp4 file. I’m not sure how to reference the asset within a script or how to even run the script to turn the .bytes file back into an mp4. I found this solution on stackoverflow:
“If the byte array is already a video stream, simply serialize it to the disk with the extension mp4. (If it’s an MP4 encoded stream).”
But I have no idea what that entails. I don’t know where the provided code goes either, nor how to run that code and reference the bytes asset within it. This is the code they provided:
Stream t = new FileStream("video.mp4", FileMode.Create);
BinaryWriter b = new BinaryWriter(t);
b.Write(videoData);
t.Close();
What do I do with this? What do I replace “video.mp4” with? If someone could walk a complete noob through this process I would be greatly appreciative.
Hi!
If the file really contains a mp4, can you try simply renaming it so it has a .mp4 extension and see what happens?
You can then validate that it’s a legit mp4 file by trying to play it in your platform’s media player, or put this file in Unity’s Asset folder and see if it’s properly recognized as a movie file.
If it turns out the movie is not “exactly” a mp4, then you will need to use more low level tools to find out what exactly is the nature of the file you have. On the OSX (or Linux) command-line, assuming your file is named “my_mysterious_file.bytes”, the following command may tell you a bit more what the file is
file my_mysterious_file.bytes
It will reveal, for example, if the file is a zip file (in which case you should first unzip it). Or it could be that it’s a lower-level media container like a mpeg elemental stream, in which case you can use a tool like ffmpeg to “re-package” it into an actual mp4.
The code that was provided to you is what you’d write in a Unity script at the point where you have received a hypothetical array of bytes (named videoData), in order to write those bytes into a file named video.mp4. If this does indeed match your use case, then this code would produce a file on disk in the running application’s current directory. If you are unsure what this is, you can always use an absolute path (e.g.: “C:\Users\asisman\video.mp4”, assuming you’re using Windows).
So let us know what your experiments reveal and we can certainly help you unwrap the mp4.
Hope this helps,
Dominique Leroux
A/V developer at Unity