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?

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.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New

We're in Beta

About us Mission Statement