0

I would like to make a web application where a user can:

  • click a button on a webpage to create a **random video (with moviepy)
  • play that video back on the webpage
  • click the button again to create a new **random video that replaces the existing one

** The random video will be created with a python/moviepy script that downloads a bunch of random video clips from the internet to a directory on my computer. It then compiles them into one video (.mp4 file).

I've done the python script bit already which successfully creates the video file.

To make the web app bit I have been recommended django and that's where I'm stuck!

So far I have installed django and got to grips with the basics .. I have a home page that says "Hello world".

My question is where do I go from here? How do I connect my python/moviepy script with django? What steps, apps, components etc, within django should I look into to make this thing?

I'm new to coding and looking for some guidance.

Thanks!

flexkwando
  • 53
  • 1
  • 7

2 Answers2

2

If you create a model with FileField then your moviepy script should upload videos into that field, that field can save the video in a specified directory in MEDIA_ROOT(you can store your post based on date) then that field will store the URL to it (you need to specify MEDIA_URL in settings.py). You can define some sort of IDs to them, if video privacy is not important, then you can use the model IDs. These IDs can be obtained trough Path converters .

At the client-side, javascript is needed. Simply running the script in view is possible, but then the user will need to wait for the response (and the browser should run into time-out). You should look at server-sent events. With Vue.js you can easily display a loading element while waiting for the event (a video to be generated) and then download and switch to the video (see Django CRUD application tutorials). The python script can run asynchronously (you call it in the view).

It is a lot, I know. Actually I'm going to learn these now, sorry for mistakes.

Levente Simofi
  • 307
  • 3
  • 11
  • I do not exactly know SSE, I need to work my way through it, but as I know you need a view for that too, where you can call your moviepy script. That view will be triggered when the page is loaded and initializes the SEE. – Levente Simofi May 27 '20 at 18:50
  • Thanks this will help me when I get to that stage. – flexkwando May 28 '20 at 16:38
1

As I see, Django is for complex sites, you should look for Flask instead. (I am learning Django and I know nothing about Flask, so I`ll go with it)

Here is the needed setup:

  • define the urlpatterns for handling the URLs
  • create a model for storing your video
  • create a django template for your page (html)
  • define a view for rendering out the template (passing the video)
  • maybe some css to design it

You can run your video generator(in the view) at every reload and override existing video(in this case you don't even need a model) or you can save the generated videos and capture the IDs in the URLs (for example: https://yoursite.com/1), in this case, the videos remain shareable.

If you go with the first option and sharing videos is not important for you then you could write a simple html page with a video and a button. The button can trigger a javascript function to run the video generator python script and refresh the page(the video is overridden), you may need to wait to the script otherwise the old video can load.

Restful API is a more advanced way to refresh the video, without reloading the page.

After these, you can deploy your page using an Apache server for example.

I tried to give you some guidelines (I am learning this on my own)

Hope that it helped :) There are tutorials for these.

Levente Simofi
  • 307
  • 3
  • 11
  • This can help you in running that python script from js(you can use response object from flask): https://stackoverflow.com/questions/32288722/call-python-function-from-js – Levente Simofi May 27 '20 at 16:36
  • Waiting for the python script: https://www.sitepoint.com/delay-sleep-pause-wait/ – Levente Simofi May 27 '20 at 16:41
  • This will reload the page: location.reload(); – Levente Simofi May 27 '20 at 16:41
  • This is incredibly helpful thank you. I am hoping it's okay to stick with django as I already spend some hours learning the basics. I will work my way through your suggested approach. One more question how can I manage multiple users ie if more than one person is on the page and refreshes? – flexkwando May 27 '20 at 17:12