Reputation: 33
I am trying to figure out how to take a string that is the file contents of a mp4 file and write a properly formatted mp4 file. Currently I am just throwing the string into a file and slapping a .mp4 extension on it, but this resulting file cannot be played by any video players (I am assuming because of all the missing meta data).
def write_mp4(mp4_string)
file = File.new('movie.mp4', 'w')
file.puts(mp4_string)
file.close
end
For context, I am doing this in a Ruby on Rails application, not sure if that changes anything. Please help thanks.
Upvotes: 3
Views: 594
Reputation: 198314
"wb"
mode, which will suppress EOL conversions and set the encoding properlywrite
, not puts
, as the latter inserts an extra EOLFile.write('movie.mp4', mp4_string, mode: 'wb')
- or even File.binwrite('movie.mp4', mp4_string)
.Of course, make sure the string actually contains a correct file before - for example, if mp4_string.encoding
doesn't return #<Encoding:ASCII-8BIT>
, you probably done goofed somewhere before the writing step, too :)
Upvotes: 3