Traditional methods for converting documents to video, such as recording a video based on the document, are time-consuming and often require professional video editing skills. Large Language Model (LLM) technology offers a more efficient and intelligent solution. This guide demonstrates how to automatically convert documents into videos using LLMs and multimodal application techniques. It includes a complete code package to help you get started quickly.
Demo
With this hands-on tutorial and the complete code, you can convert a document into a video with the help of LLMs. The generated video contains complete text, images, voiceover, subtitles, and other content. The following is an example. The input document is Lesson 1 of Chapter 1 of the Alibaba Cloud LLM Engineer ACA certification course, Understanding LLMs, and the output video is demonstrated as follows:
Lesson 1 of Chapter 1 of the Alibaba Cloud LLM Engineering ACA certification course: Understanding LLMs.
Solution overview
- Document slicing: First, we use an LLM to summarize the document titles and divide the document into different sections, and generate a summarizing section title for each section.
- Generate presentation slides: Next, we integrate the content of each part, including titles, body text, and images, and use these materials to generate presentation slide images.
- Generate narration audio and subtitles: Then, we use multimodal LLM technology to convert the text materials into audio files and automatically generate matching text subtitles based on the audio playback duration.
- Generate the video: Finally, we edit all the presentation slide images into a video and embed the audio and subtitle files into the video.
Preparations
-
Obtain and configure an API Key, which is used to call the LLMs provided by Alibaba Cloud Model Studio.
Model Studio provides a free quota for new users. After the quota is used up, billing is based on tokens. You can view Billing details for billing details.
- Configure environment variables to reduce the risk of API-KEY leakage. You can refer to Configure an API key through environment variables and choose an environment variable configuration method that suits your operating system.
-
This hands-on tutorial depends on the FFmpeg audio and video processing tool and the Marp presentation tool. Install the two tools with the following sample code:
- MacOS
- Windows
- Image generation in this hands-on tutorial relies on browser engine rendering. Make sure that a browser application such as Chromium, Google Chrome, or Microsoft Edge is installed in your working environment.
-
This hands-on tutorial is based on Python code. Make sure that Python has been installed in your working environment, and install the Python libraries required for this tutorial. The sample code is as follows:
- MacOS
- Windows
Quick start
If you want to try it out quickly, you can directly download the complete code provided in this tutorial to your local working environment and run the following commands locally:
- MacOS
- Windows
Step 1: Prepare materials
Write the text, image links in Markdown format, and other content from the document into a Markdown file, name it in the format of section_1.md, and save it to the input folder. We will use the section_1.md file provided below as the sample input document.
section_1.md
section_1.md
You need to create a Python file named main.py in your working environment directory, so that you can set parameters and call functions. The sample code is as follows:
main.py
main.py
Step 2: Document slicing
In this part, we use an LLM to generate document titles and divide the input document into sections, then use the LLM to summarize the content of each section and automatically generate a corresponding section title for each section.
Divide the text into sections
We use the API to call the Qwen-Plus LLM of the Qwen series provided by Alibaba Cloud Model Studio to generate a document title for the input document and split it into sections.
Qwen-Plus is an enhanced version of the Qwen ultra-large language model, supporting input in various languages such as Chinese and English. It offers balanced capabilities, with reasoning performance and speed between Qwen-Max and Qwen-Flash, making it suitable for medium-complexity tasks. You can also select an appropriate LLM based on your actual application requirements. For more information, see the model list.
The input and output costs of Qwen-Plus are CNY 0.004 per 1,000 tokens and CNY 0.012 per 1,000 tokens respectively. New users receive a free quota of 1 million tokens within 30 days after activating Model Studio.Create a Python file named theme_generate.py. The code example is as follows:
theme_generate.py
theme_generate.py
Import and call theme_generate_with_qwen_plus
Import and call theme_generate_with_qwen_plus
Title of the sample document: LLMs: Impact and Applications.Next, create a Python file named doc_split.py. The code example is as follows:
doc_split.py
doc_split.py
Import and call doc_split.py
Import and call doc_split.py
Output section_1.json. The "title" field is the section title, the "content" field is the section content, and image links are saved in separate fields.
Distill content
Next, we call the Model Studio platform LLM Qwen-Plus via the API to summarize and distill the content of each section.
Create a Python file named qwen_plus_marp.py. The code example is as follows:
qwen_plus_marp.py
qwen_plus_marp.py
- Text content: distill the key points and output them in Markdown format.
- Image links: output the link directly.
Step 3: Generate the presentation
In this part, we consolidate the image and text materials into a Markdown file and generate presentation images.
Before introducing the detailed process and code, we will first briefly introduce the tool used in this part: Marp. Marp is an open-source presentation authoring tool based on Markdown syntax. You can generate polished presentations simply by editing Markdown text. If you are a VS Code user, you can also download and install the Marp for VS Code plug-in for real-time preview. You can also refer to the Marp official documentation to create presentations with your own unique style.
Search for and install Marp for VS Code in the VS Code extensions.Since Marp uses specific format specifications and extended syntax when converting Markdown to presentations, we have prepared a Python script, markdown_gather.py, to aggregate functions that optimize and adjust Markdown-formatted content. The code example is as follows:
markdown_gather.py
markdown_gather.py
Consolidate image and text materials
We will call the function call_with_stream introduced in the "Distill content" part of Step 2 to obtain the key points, titles, image links, and other content of each section of the document, consolidate them in Markdown format, and output a Markdown file. Create a function file named json2md.py. The code example is as follows:
json2md.py
json2md.py
- Consolidate the title and text content of the same section together with its corresponding image links;
- Use "---" to separate the content of different sections to generate multiple presentation images.
- Set the presentation background.
Import and call the functions in json2md.py
Import and call the functions in json2md.py
Import and call the functions in markdown_gather.py
Import and call the functions in markdown_gather.py
Example output section_1.md after material integration.
Generate the presentation
Next, we generate the presentation based on Marp. Before using Marp to generate the presentation, we need to define the overall style and global styles of Marp. We can set them at the top of the Markdown file, for example:
- marp: true indicates that the Markdown file will be treated as a Marp presentation;
- theme: gaia indicates that the Marp theme is set to gaia (one of the official Marp themes).
Load the style file style.md
Load the style file style.md
Example Markdown output after loading the style file style.md.After obtaining the above output, if you are a VS Code user and have installed the Marp for VS Code plug-in, you can preview the output of the Marp presentation in real time. Click the preview icon in the upper-right corner of the interface:
Click the preview icon in the upper-left corner of the VS Code interface.The real-time preview is as follows:
Demonstration of the Marp real-time preview effect.After confirming through the preview that the output of the Marp presentation is correct, we export it as images. Create a new Python file named marp2image.py. The example code is as follows:
marp2image.py
marp2image.py
Import and call the function convert_md_files_to_png
Import and call the function convert_md_files_to_png
Example image.
Step 4: Generate narration audio and subtitles
In this part, we use a multimodal speech LLM to synthesize the text content into audio, accurately calculate the audio duration, and then automatically generate the corresponding subtitle timestamps.
Synthesize text into audio
We divide the document content into several sentences, then call the speech synthesis LLM CosyVoice provided by Alibaba Cloud Bailian through the API to synthesize all sentences into audio files in mp3 format. Create a new Python file named audio_generate_each_sentence.py. The example code is as follows:
audio_generate_each_sentence.py
audio_generate_each_sentence.py
- process_json_file: divides the document content into several sentences.
- synthesize_md_to_speech: calls the speech synthesis model CosyVoice provided by Bailian through the API to synthesize all sentences into audio files in mp3 format.
Import and reference process_json_file and synthesize_md_to_speech
Import and reference process_json_file and synthesize_md_to_speech
The text content is: Have you ever had trouble finding useful information when searching for something with a search engine?
Generate subtitles
Next, we will generate an SRT-format subtitle file by reading the duration of the audio and its corresponding text content. Create a new Python file named srt_generate_for_each_sentence.py. The code example is as follows:
srt_generate_for_each_sentence.py
srt_generate_for_each_sentence.py
Import and call the function generate_srt_from_audio
Import and call the function generate_srt_from_audio

Step 5: Generate the video
In this part, we will edit the generated presentation slides into a video, and embed the audio file and subtitle file into the video.
Edit the video
First, we calculate the duration of each presentation slide in the video. Create a new Python file named calculate_durations_for_each_image.py. The code example is as follows:
calculate_durations_for_each_image.py
calculate_durations_for_each_image.py
Import and call the function calculate_audio_durations
Import and call the function calculate_audio_durations
movie_editor.py
movie_editor.py
Import and call the function images_to_video_with_durations
Import and call the function images_to_video_with_durations
The video above does not contain audio or subtitles.
Embed audio and subtitles
Next, we will embed the audio file into the video. Create a new function file named audio2video.py. The code example is as follows:
audio2video.py
audio2video.py
Import and call the function merge_audio_and_add_to_video
Import and call the function merge_audio_and_add_to_video
srt2video.py
srt2video.py
Import and call the function merge_video_and_subtitle
Import and call the function merge_video_and_subtitle
This is the final output video, with audio and subtitles.
Step 6 (optional): Generate video from long documents
You have now built a complete document-to-video project and converted the sample document section_1.md into a video. In practice, you may need to convert longer documents, such as the first lesson of Chapter 1 of the Alibaba Cloud Large Language Model Engineer ACA Certification course: Understanding Large Language Models. We recommend splitting long documents into shorter ones, generating a video for each part, and then merging them into a single video.
Split the document
You can split the long document into a sequence of shorter documents and save them in the input folder. The file names must follow the format section_index.md, where index is the sequential number of the document. The following image shows an example:

Generate video from long document
You can create a Python file named merge_all_videos.py. The sample code is as follows:
merge_all_videos.py
merge_all_videos.py
run.sh
run.sh
Summary
With this hands-on guide, you will be able to:
- Use large language models, multimodal applications, and Marp to convert rich-text documents into engaging presentation videos.
- Walk through the end-to-end document-to-video workflow using our complete code package.
- Customize input documents, Marp style files, and render assets to create videos in your unique style.