Search code examples
pythondjangohtml5-videomultimedia

Returning a file path in Django without the file extension


I'm creating a multimedia application on my Django site that allows users to upload photos, videos and audio - the files get encoded on a 3rd party service (mp4, ogg, webm and flv for videos), and then saved to the user's bucket on Amazon S3. I'm specifically looking for an elegant solution to display the user's videos in HTML5 format. Here's how my model looks:

class Video(models.Model):
    file = models.FileField(upload_to = user_video_folder)
    owner = models.ForeignKey(User)
    video_title = models.CharField(max_length=100)
    video_caption = models.CharField(max_length=150, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    def __unicode__(self):
    return self.video_title

Then the view:

def ViewAllVideos(request):
    videos = Video.objects.filter(owner = request.user)
    template = 'site/media-videos.html'
    return render_to_response(template, {'videos': videos}, context_instance=RequestContext(request))

In the template, I need to print the path for the multiple file formats that are generated during encoding, and was hoping to not have to add a column for each format in my model, so I was wondering if there was a way to return the file path WITH the file name, but without the extension, so it would like like so in the template:

<video width="900" height="506" preload="" controls="" autoplay="">
    <source src="{{ STATIC_URL }}{{ video.file }}.mp4" type="video/mp4;">
    <source src="{{ STATIC_URL }}{{ video.file }}.ogg" type="video/ogg;">
    <source src="{{ STATIC_URL }}{{ video.file }}.webm" type="video/webm;">
    <object width="900" height="506" type="application/x-shockwave-flash" data="/foo/bar/flowplayer/flowplayer-3.2.5.swf">
        <param name="movie" value="/foo/bar/flowplayer/flowplayer-3.2.5.swf"><param name="allowfullscreen" value="true">
        <param value="config={"clip": {"url": "{{ STATIC_URL }}{{ video.file }}.flv", "autoPlay":false, "autoBuffering":true}}" name="flashvars">
    </object>
</video>

Any help is appreciated! Cheers!


Here's my new view:

def ViewAllVideos(request):
    videos = Video.objects.filter(owner = request.user)
    filenames = [os.path.splitext(os.path.basename(video.file))[0]
        for video in videos]
    context = {
        'videos': videos,
        'filenames': filenames,}
    template = 'site/media-videos.html'
    return render_to_response(template, context, context_instance=RequestContext(request))

Solution

  • You could add something like the below to your model, then use {{video.file_minus_extension}}

    def file_minus_extension(self): 
        basename, extension = os.path.splitext(self.file.url) 
        return basename
    

    EDIT: as per comment, just return filename

    def filename_minus_extension(self): 
        basename, extension = os.path.splitext(os.path.basename(self.file.name)) 
        return basename