0

I'm using moviepy to create a new movie when the user click a button on a html page. I'd like to return the new movie to user. But movie creation takes time. What can I do to send_file only when the file is created?

@app.route('/createVideo', methods=['POST'])
def createVideo():
    content = request.get_json()
    videoPromo = content['videoPromo']
    videoPerso = content['videoPerso'] 
    subclip1 = VideoFileClip(videoPromo)
    subclip2 = VideoFileClip(content['videoPerso'])
    subclip = concatenate_videoclips([subclip1, subclip2], method="compose")
    subclip.write_videofile("out.mp4",progress_bar = False)
    vid_path = os.path.join(MEDIA_PATH, 'out.mp4')
    return send_file(vid_path)

My HTML code :

    <video id="videoPromo" controls class="video-js vjs-default-skin vjs-big-play-centered">
      <source src="http://localhost:5000/var/myvideo.mp4" type="video/mp4">
    </video>

    <video id="videoPerso" controls class="video-js vjs-default-skin vjs-big-play-centered">
      <source src="http://localhost:5000/var/oceans.mp4#t=0.7" type="video/mp4">
    </video>


    <div class="btn-group" role="group" aria-label="...">
      <button class="create-video btn btn-default" type="button">Create Video</button>
    </div>
<script>

$(".create-video").click(function(){

  $.ajax({
    type: "POST",
    url: "/createVideo",
    data: JSON.stringify({  videoPerso: $("#videoPerso source").attr("src") , videoPromo: $("#videoPromo source").attr("src")}),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){                console.log(data);
},
    failure: function(errMsg) {
        alert(errMsg);
    }
});
  });
</script>
dt dino
  • 1,194
  • 6
  • 19

1 Answers1

0

If your task is taking a while and you don't want to make the user wait (and block the python interpreter!), you can return a temporary result, saying something like 'processing...' and then fire off a background task using some sort of task queue, like celery.

On your page, you can poll a certain endpoint periodically to see if the background task has finished creating the video and get back the url once it had. Having got the url back, you can show the video to the user.

dmitrybelyakov
  • 3,709
  • 2
  • 22
  • 26