【问题标题】:Is there a way to convert HTML div to a video format (MP4 or any other) in Python/Django?有没有办法在 Python/Django 中将 HTML div 转换为视频格式(MP4 或任何其他格式)?
【发布时间】:2022-01-23 02:12:15
【问题描述】:

我正在尝试呈现 HTML 页面并在其中使用特定的 <div> 将其转换为视频格式。

说明:

我知道 HTML 是静态内容,但我有必要将其转换为视频格式(这是一项要求)。我需要知道是否有一种方法可以渲染页面并将其导出为视频格式。它可以是直接的 HTML 到 MP4 的转换,也可以将渲染的 div(不记录画布)捕获为图像,然后将该图像转换为视频格式。

技术栈:
姜戈
Django 模板
HTML
Javascript

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript python html jquery django


    【解决方案1】:

    这是一个粗略的过程: (请注意,我只是从我的代码中复制了“想法”并删除了我的特定内容,因此可能存在拼写错误或小不一致)

    import imgkit
    from moviepy import editor
    
    # Step 1: render html to PNG
    
    context = { ....}
    rendered_html = Render_to_string('path/template.html', context)
    
    
    # make sure that 'width' is set otherwise it will use a min width that maybe does not fit to your html
    options = {'format': 'png', 'width': 670, 'disable-smart-width': ''}
    imgkit.from_string(rendered_html,'path/tmp.png',
                           config=config, options=options)
    
    # Step 2: create video from png and mp3
    audio = editor.AudioFileClip('path/audio.mp3')
    video = editor.ImageClip('path/tmp.png')
    video.fps = 1
    video.duration = audio.duration
    final_video = video.set_audio(audio)
    final_video.write_videofile('path/video.mp4', fps=1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-17
      • 2013-02-22
      • 2012-04-10
      • 1970-01-01
      • 2012-03-23
      • 2012-05-03
      • 2011-01-12
      • 2011-12-05
      相关资源
      最近更新 更多