hoseinisalim

hoseinisalim

Read and write streaming video

I want to read the video in a stream and after the operation on the video, save it as follows.The code below consumes a lot of time to run, and consumes a lot of CPU.

File.stream!(“play.mp4”,, chunk_size) |>
Enum.each (fn(chunk) →

# list = :binary.bin_to_list(chunk)
# listEnc = for {x, counter} <- Enum.with_index(list) do
#           x ^^^ Enum.at(listKey,rem(counter,chunk_size))
#         end
# binDec = :binary.list_to_bin(listEnc)

File.write!(“out.mp4”, chunk, [:append, :binary, :raw])
end)

What’s the solution?

First Post!

idi527

idi527

I think functions from the Enum module all all eager, maybe you can try Stream module instead. File.write! is eager as well, I think. Try IO.binwrite/2. In general, though, you’d want to operate on frames or maybe nal units for h264, which might not be what you get just by splitting the video file into chunk_size. I’d just use ffmpeg, probably for a one-off task like this.

Most Liked

voltone

voltone

It looks from your sample code (and prior questions you posted) that you’re trying to encrypt the file using a key. Your XOR is basically implementing something called ECB mode, which is not secure. For your use-case, a stream cipher would be a better fit. Erlang supports RC4 and AES-CTR, but since RC4 itself is considered weak, that leaves only AES-CTR.

Here’s how to encode a stream of data read from a binary file:

key = :crypto.strong_rand_bytes(16)
iv = :crypto.strong_rand_bytes(16)

out = File.open!("play.mp4.encrypted", [:binary, :write])
IO.binwrite(out, iv)
File.stream!("play.mp4", [], 1024) |> Enum.reduce(:crypto.stream_init(:aes_ctr, key, iv), fn chunk, state ->
  {new_state, data} = :crypto.stream_encrypt(state, chunk)
  IO.binwrite(out, data)
  new_state
end)
File.close(out)

To decode the file:

key = "???" # same key as before
out = File.open!("play2.mp4", [:binary, :write])
source = File.open!("play.mp4.encrypted", [:binary])
iv = IO.binread(source, 16)
IO.binstream(source, 1024) |> Enum.reduce(:crypto.stream_init(:aes_ctr, key, iv), fn chunk, state ->
  {new_state, data} = :crypto.stream_decrypt(state, chunk)
  IO.binwrite(out, data)
  new_state
end)
File.close(source)
File.close(out)
aseigo

aseigo

IO.write/2 expects character data / strings. You have arbitrary binary data and that does not map properly to that assumption. You want IO.binwrite/2.

That said, if you are doing significant multimedia work, you may wish to check out Membrane Framework in case it is useful / applicable to your needs.

Last Post!

hoseinisalim

hoseinisalim

Thank you very much for your help and code but I have to implement the XOR method.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement