javascriptnode.jshtml5-videocodecmedia-source

Using Node.js Get Video Codec For MediaSource API Of Random Video Upon Request


I am attempting to use the MediaSource API to stream videos to the browser, but the MediaSource API seems to require the video codec information, and this information is not known in advance.

I have to look up this information when the video is requested, and the video format is unknown in advance. The video format could be MP4, WEBM, OGG, etc. I can't find a reliable method to lookup codec information when the video is requested.

Using the ffmpeg NPM module returns a codec string that is not in a format that the MediaSource API can use.

FFMPEG Returned Codec String Example:

H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10

MP4Box seems to return a codec string that can be used with the MediaSource API, but only works for MP4 video files, and returns nothing for other formats.

MP4Box Returned Codec String Example:

video/mp4; codecs="avc1.640028,mp4a.40.2

I'm looking for a method or module to reliably lookup video codec information for a videos. I need to look up the codec when the video is requested, and the video could be any format. The format might not even be compatible with the MediaSource API or might not play in the browser at all. I won't know this information in advance.

(Possibly the MediaSource API is not the best option for what I am trying to do. This is essentially a video streaming application like Plex, so the media could be multiple different formats, and information won't be known in advance)


Solution

  • If you use ffmpeg info it will return info on the codecs in use - e.g (Big Bug Bunny example):

    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4':
    
    Metadata:
    
    major_brand : mp42
    
    minor_version : 1
    
    compatible_brands: mp42avc1
    
    creation_time : 2010-02-09T01:55:39.000000Z
    
    Duration: 00:01:00.10, start: 0.000000, bitrate: 733 kb/s
    
    Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 22050 Hz, stereo, fltp, 65 kb/s (default)
    
    Metadata:
    
    creation_time : 2010-02-09T01:55:39.000000Z
    
    handler_name : Apple Sound Media Handler
    
    Stream #0:1(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt709), 640x360, 612 kb/s, 23.96 fps, 24 tbr, 600 tbn, 1200 tbc (default)
    
    Metadata:
    
    creation_time : 2010-02-09T01:55:39.000000Z
    
    handler_name : Apple Video Media Handler
    
    Stream #0:2(eng): Data: none (rtp / 0x20707472), 45 kb/s
    
    Metadata:
    
    creation_time : 2010-02-09T01:55:39.000000Z
    
    handler_name : hint media handler
    
    Stream #0:3(eng): Data: none (rtp / 0x20707472), 5 kb/s
    
    Metadata:
    
    creation_time : 2010-02-09T01:55:39.000000Z
    
    handler_name : hint media handler
    

    This will give you the video codec and container - avc1 and mp4a in this case. I think you will find this is enough for you needs - i.e. you don't need the version numbers.

    There are various ffmpeg wrappers for nodejs, and also higher level abstraction libraries like:

    The above ones includes an option 'codec-data'.