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.
# 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)
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.
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: