How to serve a video(s) file from Django app to play in the browser?

10.4k views Asked by At

I am build a movie search web application (using Django) that will basically take a folder of movie files that end with (.mp4, .avi, .mpg etc). A MySQL database get filled with information about the movie when I run the population script.

One of the columns are the file_path of the movie. I have a template that will show some basic information for the movie with a play button next to it.

When you click the play button, it will take it you to another page (using the slug of the movie title) and you'll see an embedded video.

PROBLEM: Is that the movie doesn't play when I click the play button the embed video. The file_path to the video is stored in the database which is then passed into the django template (so I can see the path).

Is there some way to let django access these movie file paths so that they may play in the browser? (or using the default linux media player).

P.S. The movie file paths are stored in the MySQL database.

Here are the models:

class Film(models.Model):
    title = models.CharField(max_length=128, default='Blank', help_text='film title')
    year = models.CharField(max_length=15, help_text='release year', blank=True, null=True)
    rated = models.CharField(max_length=15, default=1, null=True, blank=True)
    released = models.CharField(max_length=128, default='Blank', help_text='release date')
    runtime = models.CharField(max_length=15, default='Blank', help_text='film length')
    genre = models.ManyToManyField(Genre, default=1, blank=True)
    director = models.ManyToManyField(Director, default=1, blank=True)
    type = models.CharField(max_length=20, default='filmdb', help_text='series, movie etc')
    actor = models.ManyToManyField(Actor, blank=True, verbose_name='Actor/Actress')
    writer = models.ManyToManyField(Writer, blank=True, verbose_name='Writer')
    award = models.CharField(max_length=128, default='Blank', null=True,help_text='film awards')
    country = models.ManyToManyField(Country, default=1, blank=True)
    language = models.ManyToManyField(Language)
    plot = models.TextField(max_length=256, help_text="Film plot", null=True, blank=True)
    poster = models.URLField(max_length=256, help_text='link to poster image', blank=True, null=True)
    imdb_id = models.CharField(max_length=15, default=1, null=True, blank=True)
    imdb_rating = models.CharField(max_length=15, null=True, blank=True)
    meta_score = models.CharField(max_length=10, null=True,blank=True)
    file_path = models.CharField(max_length=255, null=True, blank=True, default='N/A')
    slug = models.SlugField(unique=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(Film, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.title + self.year
2

There are 2 answers

10
Brandon Taylor On

Without knowing if the path is absolute or relative, you should be using:

<video width="320" height="240" autoplay>
    <source src="{{ movie.file_path }}" type="video/mp4"> Your browser does not support the video tag.
</video>

Except, I would be using a relative path, not an absolute path. These files have to be served by something like Apache, Nginx or Django's built-in server for development.

The paths to these files really should be relative to the directory their being served from. If you're uploading these files via Django admin for example, then they're being uploaded to the MEDIA_ROOT, and their .url attribute will be relative to that. E.g.:

/movie/top_gun.mp4

For most sites, I typically put the MEDIA_ROOT under the STATIC_ROOT, so I can serve them easily in development or production.

0
Yemi Bold On

If it finds the path, then add .url at the end of your jinja like code in your template html and must be in a video tag, like below:

<video controls src="{{ Tutorials.attachments.url }}"></video>