Base64 encoding and decoding for mp4 video in Ruby

Praaveen Vr
praaveen
Published in
Jan 6, 2018

Encoding

require 'base64'
data = File.open('/home/name/folder/sample-video.mp4').read
encoded = Base64.encode64(data)

encoded output looks like long string based on the file size.

/9j/4AAQSkZJRgABAQAAAQABAAD/4gKgSUNDX1BST0ZJTEUAAQEAAAKQbGNt\ncwQwAABtbnRyUkdCIFhZWiAH4QALAAkACQAYABthY3NwQVBQTAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLWxjbXMAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAtkZXNjAAABCAAA\nADhjcHJ0AAABQAAAAE53dHB0AAABkAAAABRjaGFkAAABpAAAACxyWFlaAAAB\n0AAAABRiWFlaAAAB5AAAABRnWFlaAAAB+AAAABR...........................................................

Decoding

File.open("/home/name/received/sample.mp4", "wb") do |file|    file.write(Base64.decode64(encoded))
end

Output file

File can be found at the path "/home/name/received"

--

--