# Struggling to read a grayscale video in

**URL:** <https://discourse.julialang.org/t/struggling-to-read-a-grayscale-video-in/17373>\
**Category:** General Usage\
**Created:** [November 9, 2018, 11:48pm UTC](https://discourse.julialang.org/t/struggling-to-read-a-grayscale-video-in/17373 "2018-11-09T23:48:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [November 9, 2018, 11:48pm UTC](https://discourse.julialang.org/t/struggling-to-read-a-grayscale-video-in/17373/1 "2018-11-09T23:48:50Z")

</div>

I’m struggling to figure out how to read in a UInt8 grayscale video.

1. `read(open(videofilename))` isn’t working on this video format. I just get a 1D array of UInt8 that’s about 20k elements longer than the video should be based on `w x h x nframes`
2. `VideoIO.jl` doesn’t support current FFMPEG versions, and I can’t make it use older versions [Precompilation LoadError: could not open file ...src/ffmpeg/AVUtil/v56/LIBAVUTIL.jl · Issue #131 · JuliaIO/VideoIO.jl · GitHub](https://github.com/kmsquire/VideoIO.jl/issues/131)
3. `FFmpegPipe.jl` gets close but seems like forces reading the single channel video as RGB channels, resulting in a frame that’s 3x taller than it should be (using 3 successive frames, from what I can tell). [readpngdata for grayscale · Issue #4 · perrutquist/FFmpegPipe.jl · GitHub](https://github.com/perrutquist/FFmpegPipe.jl/issues/4)

Any ideas appreciated. I’d like most to have a basic function where I can tweak ffmpeg to match the source format and control the codec precisely. I’m writing videos like that based on the example given here: [Creating a video from a stack of Images - #8 by stillyslalom](https://discourse.julialang.org/t/creating-a-video-from-a-stack-of-images/646/8)

---

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [November 10, 2018, 12:51am UTC](https://discourse.julialang.org/t/struggling-to-read-a-grayscale-video-in/17373/2 "2018-11-10T00:51:57Z")

</div>

Also, FileIO isn’t able to read the video:

```julia
using FileIO
obj = load("test.avi")

```

```julia
Error encountered while loading "test.avi". Fatal error:

DelegateFailed `'ffmpeg' -nostdin -v -1 -vframes %S -i '%i' -vcodec pam -an -f rawvideo -y '%u.pam' 2&gt; '%Z'' @ error/delegate.c/InvokeDelegate/1919

```

---

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [November 10, 2018, 4:51am UTC](https://discourse.julialang.org/t/struggling-to-read-a-grayscale-video-in/17373/3 "2018-11-10T04:51:48Z")

</div>

It seems that the 3x taller issue with FFmpegPipe.jl was actually because of an oddity in how Image J saves it’s AVIs, with 3 duplicated grayscale channels, or something to that effect.

I’ve hardcoded a stride into my FFmpegPipe code for now and it seems to work.

But it is VERY slow… Lots of small readbytes() seem to be slowing things down significantly

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [November 10, 2018, 2:58pm UTC](https://discourse.julialang.org/t/struggling-to-read-a-grayscale-video-in/17373/4 "2018-11-10T14:58:43Z")

</div>

> [@ianshmean](#):
>
> But it is VERY slow… Lots of small readbytes() seem to be slowing things down significantly

Yes, as I mentioned in the issue, the `readpngdata` could be much better implemented using `IOBuffer`s. I hadn’t properly learned how to use these in Julia at the time that I wrote it. (And read speed didn’t matter for what I needed it for.)

However, I suspect a lot of the slowdown comes from the fact that each frame is compressed as a png and then immediately decompressed. If you want to write something faster, it’s proabably be better to go with an uncompressed intermediate format.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [November 10, 2018, 4:05pm UTC](https://discourse.julialang.org/t/struggling-to-read-a-grayscale-video-in/17373/5 "2018-11-10T16:05:26Z")

</div>

> [@ianshmean](#):
>
> I’ve hardcoded a stride into my FFmpegPipe code for now and it seems to work.

Better would be to reduce the output framerate of ffmpeg with the `r_out` keyword argument. That way you don’t waste time on transferring frames that will not be used.
