Generate HTML Video Player Code
Add an existing MP4 video to your webpage with a native browser player. Enter the video file’s address, choose the playback settings, and copy the generated <video> markup. The generator creates a player element with an MP4 <source>; it does not record or convert video files.
You can also create the player markup in an HTML document with Aspose.HTML for Python via .NET . The Python example follows your selected settings, while the browser handles playback when someone opens the page.
HTML Video Generator Settings
- Source (MP4) sets the video file address. Use a direct MP4 URL or a path relative to the HTML page. The generated
<source>usestype="video/mp4". - Preload gives the browser a loading hint. Choose
noneto avoid preloading,metadatato request information such as duration, orautoto allow preloading of the media. The browser may handle the hint according to its own policies. - Width and Height set optional player dimensions in pixels. Keep the video’s proportions in mind when choosing the display area.
- Controls shows the browser’s playback controls. Loop repeats the video after it ends.
- Autoplay requests playback when the video is ready. Muted sets the initial audio state to silent.
The video file must remain accessible from the published page, and its codecs must be supported by the viewer’s browser. The generator configures one MP4 source; changing a filename extension does not convert another video format to MP4. See the HTML video reference for media element behavior.
Create an HTML Video Element in Python
Use
HTMLDocument
to create a document, configure a <video> element, and append its MP4 source. The example saves an HTML file containing that structure and updates as you change the generator settings.
For a relative video path, publish the video in the corresponding location beside the HTML page. The generated Python code creates the document structure; it does not start media playback in Python.
import aspose.html
# Create an HTML document
with aspose.html.HTMLDocument() as document:
# Create the video element
video = document.create_element("video")
video.set_attribute("controls", "")
video.set_attribute("preload", "auto")
# Create an MP4 source
source = document.create_element("source")
source.set_attribute("src", "./zoo.mp4")
source.set_attribute("type", "video/mp4")
video.append_child(source)
# Append the video element to the document body
document.body.append_child(video)
document.save("video.html")
Steps to Add a Video to HTML in Python
- Create an
HTMLDocumentand usecreate_element()to create avideoelement. - Apply the selected playback attributes and optional dimensions with
set_attribute(). Boolean attributes such ascontrolsuse an empty value when enabled. - Create a
sourceelement and set itssrcto the MP4 address and itstypetovideo/mp4. - Use
append_child()to attach the source to the video, then the video todocument.body. - Save the HTML document with
save(). Thewithstatement closes the document automatically.
HTML Video Generator FAQ
1. Why is my HTML video not autoplaying?
Browsers commonly restrict autoplay with sound until a visitor interacts with the page. Enable both Autoplay and Muted for silent playback, but keep Controls available so visitors can start the video themselves. Some mobile browsers also require playsinline for inline autoplay; add that attribute to the generated markup if needed. Browser and user settings can still prevent automatic playback. See the
autoplay guide
for details.
2. Can I use a YouTube link in the HTML video generator?
A YouTube watch-page URL is not a direct video file and cannot be used as this player’s MP4 source. Use YouTube’s official embed URL inside an iframe instead. The HTML Iframe Generator creates that markup for an embedded player.
3. How do I add subtitles or captions to an HTML video?
Add a <track> element inside the generated <video> and point its src to a WebVTT file. Specify the track’s language with srclang and choose kind="subtitles" for translated dialogue or kind="captions" for dialogue and relevant sounds. The generator does not create caption files or track elements. See the
HTML track reference
for the required attributes.
Install Aspose.HTML for Python via .NET
Install the aspose-html-net package from
PyPI
.
Install Aspose.HTML for Python via .NET
pip install aspose-html-net
Review the installation guide and system requirements before deployment. Request a temporary license to test the API without evaluation limitations.
Related HTML Generators for Python
Add audio players, embedded pages, images, and other HTML elements.