Search code examples
pythonffmpegpython-requestsbinaryfiles

How to edit Video metadata while in Binary State using Python


I want to download a Video file from internet using requests library and before saving that endit metadata of the video.

import requests

url = 'https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_5mb.mp4'

r = requests.get(url, stream=True)

with open('video.mp4', 'wb') as file:
     file.write(r.content)

I just want to change the metadata to video before saving the file.


Solution

  • I don't think that's possible. My approach would be to first download the video and then consider using a library such as tagpy or mutagen.

    I would recommend mutagen since I find that it has a good documentation

    See here on installation steps for mutagen

    Example code using mutagen

    >>> import mutagen
    >>> mutagen.File("11. The Way It Is.ogg")
    {'album': [u'Always Outnumbered, Never Outgunned'],
     'title': [u'The Way It Is'], 'artist': [u'The Prodigy'],
     'tracktotal': [u'12'], 'albumartist': [u'The Prodigy'],'date': [u'2004'],
     'tracknumber': [u'11'],
    >>> _.info.pprint()
    u'Ogg Vorbis, 346.43 seconds, 499821 bps'
    >>>
    

    And then to change the title, you simply access the dictionary key and change the value of it

    from mutagen.flac import FLAC
    
    audio = FLAC("example.flac")
    audio["title"] = u"An example"
    audio.pprint()
    audio.save()