# SVG to RGB: How to save a series of svg images to video?

**URL:** https://discourse.julialang.org/t/svg-to-rgb-how-to-save-a-series-of-svg-images-to-video/85309
**Category:** General Usage
**Tags:** images, video
**Created:** [August 4, 2022, 5:39pm UTC](https://discourse.julialang.org/t/svg-to-rgb-how-to-save-a-series-of-svg-images-to-video/85309 "2022-08-04T17:39:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rakeshvar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakeshvar/32/3613_2.png) [@rakeshvar](https://discourse.julialang.org/u/rakeshvar)
#### Post date: [August 4, 2022, 5:39pm UTC](https://discourse.julialang.org/t/svg-to-rgb-how-to-save-a-series-of-svg-images-to-video/85309/1 "2022-08-04T17:39:48Z")

</div>

My code generates a series of svg images as text. Now I want to save them as an animated video.  
Here is a MWE.

```julia
svgtxt = 
"""
<svg width="100%" height="100%" viewBox="30 57 1102 582">
<g style="stroke:#f00; stroke-width:5;">
<path fill="#D7DDDF" d="M476,257 L135,257 L411,57 Z"/>
<path fill="#D2DFE4" d="M857,381 L751,57 L1027,257 Z"/>
<path fill="#D9E0E1" d="M240,581 L135,257 L30,581 Z"/>
<path fill="#B7B9B9" d="M476,257 L751,57 L411,57 Z"/>
<path fill="#D0DEE2" d="M856,381 L1132,581 L1027,257 Z"/>
</g>
</svg>
"""
display("image/svg+xml", svgtxt)

```

![output_1_0](https://global.discourse-cdn.com/julialang/original/3X/7/d/7df465fd10a5909998974f906f6493523463b111.png)

Now I want to save this svg to a video as frames. (Based on [this post](https://discourse.julialang.org/t/creating-a-video-from-a-stack-of-images/646/6).)  
(In reality I will have a yielding function that will keep returning different svgs at each call.)

```julia
open(`ffmpeg -loglevel warning -y -f rawvideo -pix_fmt rgb24 -s:v 1200x1200 -r 30 -i pipe:0 -vf "transpose=0" -pix_fmt yuv420p test.mp4`, "w") do out
        for i = 1:300
            write(out, svgtxt) # How to change svgtxt to a valid RGB buffer?
        end
    end

```

gives

```julia
pipe:0: corrupt input packet in stream 0
[rawvideo @ 0x55d5ac91e040] Invalid buffer size, packet size 115200 < expected frame_size 4320000
Error while decoding stream #0:0: Invalid argument
failed process: Process(`ffmpeg -loglevel warning -y -f rawvideo -pix_fmt rgb24 -s:v 1200x1200 -r 30 -i pipe:0 -vf transpose=0 -pix_fmt yuv420p test.mp4`, ProcessExited(69)) [69]

```

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [August 4, 2022, 6:00pm UTC](https://discourse.julialang.org/t/svg-to-rgb-how-to-save-a-series-of-svg-images-to-video/85309/2 "2022-08-04T18:00:28Z")

</div>

Either [Ffmpeg support for svg rasterization - Stack Overflow](https://stackoverflow.com/questions/48216871/ffmpeg-support-for-svg-rasterization) or rasterize the SVGs before sending to ffmpeg. Something like [How to convert an svg file to png with fixed pixel size/resolution](https://discourse.julialang.org/t/how-to-convert-an-svg-file-to-png-with-fixed-pixel-size-resolution/33561)

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [August 4, 2022, 7:00pm UTC](https://discourse.julialang.org/t/svg-to-rgb-how-to-save-a-series-of-svg-images-to-video/85309/3 "2022-08-04T19:00:55Z")

</div>

ffmpeg has to be compiled with svg support, I believe, and there’s probably a way of telling whether the version you’re using was or wasn’t.

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [August 5, 2022, 8:11am UTC](https://discourse.julialang.org/t/svg-to-rgb-how-to-save-a-series-of-svg-images-to-video/85309/4 "2022-08-05T08:11:08Z")

</div>

As a workaround, you could use Luxor to build an animation from SVG images like this:

```julia
using Luxor

function make_an_svg()
    cols = ["#389826", "#cb3c33", "#9558b2"]
    return """
    <svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 512 512">
    <circle fill="$(cols[rand(1:end)])" cx="256" cy="137" r="83"/>
    <circle fill="$(cols[rand(1:end)])" cx="145" cy="329" r="83"/>
    <circle fill="$(cols[rand(1:end)])" cx="367" cy="329" r="83"/></svg>"""
end 
function frame(scene, framenumber)
    background("black")
    placeimage(readsvg(make_an_svg()), O, centered=true)
end
movie = Movie(400, 400, "svg movie")
animate(movie, [Scene(movie, frame, 1:20)], 
    framerate=4, creategif=true)

```

which generates a frame for each SVG string returned by `make_an_svg()`.

![jc](https://global.discourse-cdn.com/julialang/original/3X/8/e/8e4b6edf15359bdc0399b502e57c6d3ef6ccf3c4.gif)
