Programmatic Video Editing with Python


257 points | by selvan 13 days ago


  • Daiz 12 days ago
    If you're interested in high performance video processing with Python and want something more than just a fancy ffmpeg wrapper, then I can highly recommend checking out Vapoursynth:

    It's been around for ~9 years and has a relatively large community around it, most visibly found on the Doom9 forums:

    I'm personally still mostly using Avisynth these days since it's what I'm most familiar with, but I've also used Vapoursynth and it's definitely the one to learn if you want to get into programmatic video processing these days.

  • PufPufPuf 12 days ago
    I love MoviePy! It's great when you have a lot of bits of audio, video, and text you need to glue together. I have used it to create some GPT-2-generated Peppa Pig cartoons ( -- stick the image, text, and speech together (aligned to the speech length), concat all the clips, and finally apply some fancy ffmpeg effects.
    • mark_l_watson 12 days ago
      Thanks for the comment, I also have a zillion short family and travel video clips, so I will give it a try to. BTW, your YouTube video is hilarious!
      • tclancy 12 days ago
        Oh, so you’re the guy.
      • senthil_rajasek 12 days ago
        What a nice surprise to see MoviePy on the HN front page!

        I was tinkering with moviepy over the weekend to add subtitles to my standup comedy routine. I used a hand edited simple ".srt" file. It was easy and fun to implement.

        Here is the finished result,

        • yuy910616 12 days ago
          I've always thought "from lib import star" is somewhat of a anti-pattern.

          Personally I think that makes sense - because sometimes when I read code, I just have no idea where something comes from.

          But I've seen import star everywhere, couple of examples:

          Doc for this product

          FastAI's course

          Bing ads code base

          So I'm guessing it is more of accepted than I expected? What does HackerNews think?

          • dbieber 12 days ago
            Here's my take: Don't use "import-star" in library code. Like you say, it obscures where elements of the namespace came from.

            When using Python interactively, esp. if the interactive session is to be thrown away at the end, then import-star can be fine and can be a good time-saver. Video editing is a great example of when this is appropriate. See also manim. Other examples might be one-off html parsing or one-off data manipulation tasks.

            Similar to "import-star" is multiple inheritance. Just like import-star, multiple inheritance can make it ambiguous where methods come from, and imo it should similarly be avoided by default unless there's a compelling reason to use it.

            • pbourke 12 days ago
              I don’t import-star in the console or notebooks (PyCharm, specifically) because autocomplete works better with the module to scope the search. So I end up doing a lot of aliased imports like “import numpy as np”
            • tclancy 12 days ago
              No, you’re right. It’s like picking your nose. Don’t ever do it. I don’t ever do it.

              Unless no one can see.

              Somewhat more seriously, don’t do it in anything other than one-off code once you’ve experimented enough to get the result you want unless you aren’t scared of the people who will yell at you about circular imports.

              • ackbar03 12 days ago
                You pick your nose? Ew! I never pick my nose!
              • dragonwriter 12 days ago
                > I've always thought "from lib import star" is somewhat of a anti-pattern

                Like patterns, anti-patterns are context sensitive. “from lib import *” is usually an anti-pattern, but doing it exactly once in a source file, especially a short one, and especially for demonstration code for the library so imported, is not a problem.

                But if you do it two or more times in the same source file...

                • qbasic_forever 12 days ago
                  pep8 discourages it, but context matters. a half page readme example trying to demonstrate API usage succinctly is not the sames as a 10 year old 40k line codebase that 25 devs work on
                  • amelius 12 days ago
                    Side question: how do you prevent import statements from being imported by "from lib import star"? (Without explicitly mentioning all the stuff that is not import statements).

                    For example, this is

                        import numpy as np
                        def f(x): return 2*x
                    And here it is imported:

                        from mymod import *
                        f(10) # Works as expected.
                        np.sum([10, 20]) # Works, but shouldn't work.
                    • connorbrinton 12 days ago
                      The best you can do (without doing crazy introspection) is to set `__all__` in `mymod`. It still requires listing everything you want to be exported from `mymod`, but at least you only need to do it once.


                      • formerly_proven 12 days ago
                        Linters will also complain about importing stuff that's not listed in a module's __all__, so it's overall good API hygiene to have one.
                      • ciupicri 12 days ago
                        You could use names starting with _ (underscore) for things that you don't want to export. Your module would look like this:

                            import numpy as _np  # numpy will be hidden
                            def f(x): return x * 2
                            def _g(x): return _np.array([x, x])  # _g is a non-public function
                        • qbasic_forever 12 days ago
                          Any non-trivial python module is going to be broken up into multiple files and have an which you can explicitly define what's imported and exposed. You can hoist a single file module into a folder-based module if you need direct control and don't want to split it apart into multiple files yet too.
                          • comradesmith 12 days ago
                            Python modules can define a list named `__all__` which of present will define the behaviour of importing “everything”.

                            The `from x import y` syntax is unaffected

                          • detaro 12 days ago
                            An anti-pattern, but I think it's somewhat acceptable for one-offs, examples, ... where there is really only one thing it can come from.
                          • fareesh 12 days ago
                            What are some good / mature / performant libraries to programmatically achieve compositions of text, audio and image files, perhaps with a timeline in which these can be declared at various timestamps, and the output is a video file?

                            Does this one do it?

                            • minimaxir 12 days ago
                              Essentially this repo is a robust wrapper for FFMPEG, which is what most programmatic video editing tools use in one way or another.
                              • mr-wendel 12 days ago
                                I've had a lot of fun with It seems a bit RAM hungry as you define lots (dozens?) of clips thought.

                                I found it super useful to write a quick Python script to auto-generate JSON in the format it wants, combining screenshots, headers, footers, and such into a nice demo-video.

                                • ackbar03 12 days ago
                                  Just curious, what is your use case? I had to do some stuff like this for a school project a while ago, wrangled with ffmpeg whilst cursing under my breathe. Curious what other people do this for
                                  • fareesh 12 days ago
                                    Essentially the same thing - ffmpeg syntax is very complex to maintain so I wanted to find out if there are better approaches to implementing this kind of solution.

                                    The use-case is mostly audio conversations converted to video with the relevant speaker's face shown prominently, based on timestamp data.

                                  • wizzwizz4 12 days ago
                                    AviSynth+ [0] is one of the most mature libraries around. It's performant enough to run live (usually), though I don't imagine it's heavily optimised.


                                    • Budabellly 12 days ago
                                      • qbasic_forever 12 days ago
                                        gstreamer is mature... still hard to say if it matches the other requirements (it is notoriously difficult to figure out and use from its docs)
                                      • greengrom 6 days ago
                                        Hi friend, have you tought of hiring a professional? I have used ImageRevolver, check them here , they offer services like Cinematography, Live Streaming, Video editing, 3D Modeling and more, I know you're asking how to use python for that but maybe if it's something urgent or you don't want to break your head give them a try!
                                        • mcguire 12 days ago
                                          Ya' know... This is exactly what I was looking for a few months back, when I started playing with video editing. I couldn't find anything like this, so I went with kdenlive to produce things like from things like
                                          • dukeofdoom 12 days ago
                                            Can I use python projects to do any of these in an automated way:

                                            1. Stabilize footage

                                            2. Cut out silent moments from clips with dialog.

                                            3. I have spikes in audio from handling the camera. These are very distinctive spikes. Some automated way of getting rid of these audio peaks or lowering their volume.

                                            I just want to throw the clips in a folder, and pre process them in the background, before I add them to a final cut project.

                                            • reactspa 12 days ago
                                              The 3Blue1Brown YouTube channel (a well regarded channel that publishes visualizations of various mathematical and other processes) also uses Python to programmatically generate the videos:


                                              • TchoBeer 12 days ago
                                                Note: that's for making the animations. Editing is done in a video editor, though I'm not sure which one he uses anymore
                                              • fddhjjj 12 days ago
                                                Very interesting. What libraries are out there for animations? Can I use python to make cartoon videos for my kids?
                                                • renonce 12 days ago
                                                  I happened to be looking for a programming-based video editor last night. It's hard to reuse patterns in conventional video editors. MoviePy is great but it looks like it's lacking maintainers and many wanted features are not available, such as GPU accerlation.
                                                  • popotamonga 12 days ago
                                                    Hm could this be used to make a clone of ?

                                                    Have video templates then render them on the cloud filling the template placeholders with images, text, etc.

                                                    • kall 12 days ago
                                                      If you are comfortable with javascript instead of python, I think Remotion [0] could get you to something like that pretty quickly.

                                                      It‘s more data driven motion graphics instead of non-linear video editing, but it looks like that‘s what pirsonal is doing pretty much.


                                                    • arriu 12 days ago
                                                      Anyone have any good examples of where something like this might be used?
                                                      • zwieback 12 days ago
                                                        this looks great. I normally use Premiere Elements but the overhead for short compositing is too high and there's little automation (at least I don't know how) so something like this is great. The example of compositing using regions found with a template line drawing is intriguing.
                                                        • 35mm 12 days ago
                                                          If you move to Premiere Pro and After Effects, you can automate almost everything that you might want to edit and or composite.

                                                          Both have their own scripting language based on JavaScript and After Effects also uses plain JS.

                                                          There are also a number of paid plugins which provide varying levels of no-code automation. For example there is one which connects to Google Sheets and creates a new AE comp from each spreadsheet row, replacing text, images from URLs, changing dimensions of objects, etc. all based on the values of the spreadsheet cells.

                                                        • atum47 12 days ago
                                                          I'm creating a web version of flash, the animation software, I was considering adding video support to it, so people could import video clips and make animations on top of it. Right now I'm exporting movies using the canvas capture stream, but I'm considering using ffmpeg (js) also. Cool project, I'm gonna star it so I could use it later.