# MediaReader **Repository Path**: mustanggu/media-reader ## Basic Information - **Project Name**: MediaReader - **Description**: Media metadata reader using FFprobe - **Primary Language**: TypeScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-03-25 - **Last Updated**: 2022-04-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: FFprobe, TypeScript ## README # MediaReader Media file metadata reader. # Required Dependencies * FFprobe # How to build ```shell npm install npm run build ``` # API * Media * Media.ReadFromFileSync(file) Read metadata from file synchronized Sample: ```javascript // Read media metadata synchronized const { Media } = require('dist/index.js'); const file = ''; // file path const media = Media.ReadFromFileSync(file); console.log(media); /** Console Output: Media { video: VideoStream { index: Property { value: 0 }, start_pts: Property { value: 0 }, start_time: Property { value: 0 }, duration_ts: Property { value: 5165160 }, duration: Property { value: 86.086 }, bit_rate: Property { value: 71382367 }, nb_frames: Property { value: 5160 }, codec_name: Property { value: 'hevc' }, codec_long_name: Property { value: 'H.265 / HEVC (High Efficiency Video Coding)' }, profile: Property { value: 'Main 10' }, codec_tag_string: Property { value: 'hvc1' }, codec_tag: Property { value: '0x31637668' }, r_frame_rate: Property { value: [Ratio] }, avg_frame_rate: Property { value: [Ratio] }, time_base: Property { value: [Ratio] }, disposition: Property { value: [Object] }, tags: Property { value: [Object] }, width: Property { value: 3840 }, height: Property { value: 2160 }, coded_width: Property { value: 3840 }, coded_height: Property { value: 2160 }, has_b_frames: Property { value: 1 }, level: Property { value: 153 }, refs: Property { value: 1 }, closed_captions: Property { value: '0' }, pix_fmt: Property { value: 'yuv420p10le' }, color_range: Property { value: 'tv' }, color_space: Property { value: 'bt2020nc' }, color_transfer: Property { value: 'smpte2084' }, color_primaries: Property { value: 'bt2020' }, chroma_location: Property { value: 'topleft' }, sample_aspect_ratio: Property { value: [Ratio] }, display_aspect_ratio: Property { value: [Ratio] } }, audio: AudioStream { index: Property { value: 1 }, start_pts: Property { value: 0 }, start_time: Property { value: 0 }, duration_ts: Property { value: 4132864 }, duration: Property { value: 86.101333 }, bit_rate: Property { value: 192000 }, nb_frames: Property { value: 4037 }, codec_name: Property { value: 'aac' }, codec_long_name: Property { value: 'AAC (Advanced Audio Coding)' }, profile: Property { value: 'LC' }, codec_tag_string: Property { value: 'mp4a' }, codec_tag: Property { value: '0x6134706d' }, r_frame_rate: Property { value: [Ratio] }, avg_frame_rate: Property { value: [Ratio] }, time_base: Property { value: [Ratio] }, disposition: Property { value: [Object] }, tags: Property { value: [Object] }, sample_fmt: Property { value: 'fltp' }, sample_rate: Property { value: 48000 }, channels: Property { value: 2 }, bits_per_sample: Property { value: 0 }, channel_layout: Property { value: 'stereo' } }, format: MediaFormat { filename: Property { value: 'E:\\视频文件\\HDR视频文件\\Swordsmith.mp4' }, format_name: Property { value: 'mov,mp4,m4a,3gp,3g2,mj2' }, format_long_name: Property { value: 'QuickTime / MOV' }, nb_streams: Property { value: 2 }, nb_programs: Property { value: 0 }, start_time: Property { value: 0 }, duration: Property { value: 86.101333 }, size: Property { value: 770255991 }, bit_rate: Property { value: 71567392 }, probe_score: Property { value: 100 }, tags: Property { value: [Object] } } } */ ``` * Media.ReadFromFile(file) Read metadata from file asynchronized Sample: ```javascript // Read media metadata asynchronized const { Media } = require('dist/index.js'); const file = ''; // file path Media.ReadFromFile(file) .then(media => { console.log(media); }); /** Console Output: Media { video: VideoStream { index: Property { value: 0 }, start_pts: Property { value: 0 }, start_time: Property { value: 0 }, duration_ts: Property { value: 5165160 }, duration: Property { value: 86.086 }, bit_rate: Property { value: 71382367 }, nb_frames: Property { value: 5160 }, codec_name: Property { value: 'hevc' }, codec_long_name: Property { value: 'H.265 / HEVC (High Efficiency Video Coding)' }, profile: Property { value: 'Main 10' }, codec_tag_string: Property { value: 'hvc1' }, codec_tag: Property { value: '0x31637668' }, r_frame_rate: Property { value: [Ratio] }, avg_frame_rate: Property { value: [Ratio] }, time_base: Property { value: [Ratio] }, disposition: Property { value: [Object] }, tags: Property { value: [Object] }, width: Property { value: 3840 }, height: Property { value: 2160 }, coded_width: Property { value: 3840 }, coded_height: Property { value: 2160 }, has_b_frames: Property { value: 1 }, level: Property { value: 153 }, refs: Property { value: 1 }, closed_captions: Property { value: '0' }, pix_fmt: Property { value: 'yuv420p10le' }, color_range: Property { value: 'tv' }, color_space: Property { value: 'bt2020nc' }, color_transfer: Property { value: 'smpte2084' }, color_primaries: Property { value: 'bt2020' }, chroma_location: Property { value: 'topleft' }, sample_aspect_ratio: Property { value: [Ratio] }, display_aspect_ratio: Property { value: [Ratio] } }, audio: AudioStream { index: Property { value: 1 }, start_pts: Property { value: 0 }, start_time: Property { value: 0 }, duration_ts: Property { value: 4132864 }, duration: Property { value: 86.101333 }, bit_rate: Property { value: 192000 }, nb_frames: Property { value: 4037 }, codec_name: Property { value: 'aac' }, codec_long_name: Property { value: 'AAC (Advanced Audio Coding)' }, profile: Property { value: 'LC' }, codec_tag_string: Property { value: 'mp4a' }, codec_tag: Property { value: '0x6134706d' }, r_frame_rate: Property { value: [Ratio] }, avg_frame_rate: Property { value: [Ratio] }, time_base: Property { value: [Ratio] }, disposition: Property { value: [Object] }, tags: Property { value: [Object] }, sample_fmt: Property { value: 'fltp' }, sample_rate: Property { value: 48000 }, channels: Property { value: 2 }, bits_per_sample: Property { value: 0 }, channel_layout: Property { value: 'stereo' } }, format: MediaFormat { filename: Property { value: 'E:\\视频文件\\HDR视频文件\\Swordsmith.mp4' }, format_name: Property { value: 'mov,mp4,m4a,3gp,3g2,mj2' }, format_long_name: Property { value: 'QuickTime / MOV' }, nb_streams: Property { value: 2 }, nb_programs: Property { value: 0 }, start_time: Property { value: 0 }, duration: Property { value: 86.101333 }, size: Property { value: 770255991 }, bit_rate: Property { value: 71567392 }, probe_score: Property { value: 100 }, tags: Property { value: [Object] } } } */ ``` * toObject() Transform read metadata to a simple object. Sample: ```javascript const { Media } = require('dist/index.js'); const file = ''; // file path const media = Media.ReadFromFileSync(file); console.log(media.toObject()); /** * { video: { index: 0, start_pts: 0, start_time: 0, duration_ts: 5165160, duration: 86.086, bit_rate: 71382367, nb_frames: 5160, codec_name: 'hevc', codec_long_name: 'H.265 / HEVC (High Efficiency Video Coding)', profile: 'Main 10', codec_tag_string: 'hvc1', codec_tag: '0x31637668', r_frame_rate: '60000/1001', avg_frame_rate: '60000/1001', time_base: '1/60000', disposition: { default: 1, dub: 0, original: 0, comment: 0, lyrics: 0, karaoke: 0, forced: 0, hearing_impaired: 0, visual_impaired: 0, clean_effects: 0, attached_pic: 0, timed_thumbnails: 0, captions: 0, descriptions: 0, metadata: 0, dependent: 0, still_image: 0 }, tags: { creation_time: '2016-10-24T06:29:51.000000Z', language: 'und', handler_name: 'Video Media Handler', vendor_id: '[0][0][0][0]', encoder: 'HEVC Coding' }, width: 3840, height: 2160, coded_width: 3840, coded_height: 2160, has_b_frames: 1, level: 153, refs: 1, closed_captions: '0', pix_fmt: 'yuv420p10le', color_range: 'tv', color_space: 'bt2020nc', color_transfer: 'smpte2084', color_primaries: 'bt2020', chroma_location: 'topleft', sample_aspect_ratio: '1/1', display_aspect_ratio: '16/9' }, audio: { index: 1, start_pts: 0, start_time: 0, duration_ts: 4132864, duration: 86.101333, bit_rate: 192000, nb_frames: 4037, codec_name: 'aac', codec_long_name: 'AAC (Advanced Audio Coding)', profile: 'LC', codec_tag_string: 'mp4a', codec_tag: '0x6134706d', r_frame_rate: '0/0', avg_frame_rate: '0/0', time_base: '1/48000', disposition: { default: 1, dub: 0, original: 0, comment: 0, lyrics: 0, karaoke: 0, forced: 0, hearing_impaired: 0, visual_impaired: 0, clean_effects: 0, attached_pic: 0, timed_thumbnails: 0, captions: 0, descriptions: 0, metadata: 0, dependent: 0, still_image: 0 }, tags: { creation_time: '2016-10-24T06:29:51.000000Z', language: 'eng', handler_name: 'Sound Media Handler', vendor_id: '[0][0][0][0]' }, sample_fmt: 'fltp', sample_rate: 48000, channels: 2, bits_per_sample: 0, channel_layout: 'stereo' }, format: { filename: 'E:\\视频文件\\HDR视频文件\\Swordsmith.mp4', format_name: 'mov,mp4,m4a,3gp,3g2,mj2', format_long_name: 'QuickTime / MOV', nb_streams: 2, nb_programs: 0, start_time: 0, duration: 86.101333, size: 770255991, bit_rate: 71567392, probe_score: 100, tags: { major_brand: 'isom', minor_version: '1', compatible_brands: 'isom', creation_time: '2016-10-24T05:33:14.000000Z' } } } */ ``` * FFprobe * constructor() FFprobe constructor Arguments: * bin ffprobe executable file path, default: "ffprobe" need to set PATH system enviroment * i(file) FFprobe cmd option: -i Arguments: * file Input file path * show_streams(show) FFprobe cmd option: -show_streams Arguments: * show Type:Boolean; True: do read stream metadata; False: do not read stream metadata; Default: true * show_format(show) FFprobe cmd option: -show_format Arguments: * show Type:Boolean; True: do read format metadata; False: do not read format metadata; Default: true * execute() Asynchronized execute ffprobe in cmd Return: Type: Promise\ * executeSync() Synchronized execute ffprobe in cmd Return: Type: JSON * show_entries(entries) FFprobe cmd option: -show_entries Arguments: * entries Type: string * select_streams(select) FFprobe cmd option: -select_streams Arguments: * select Type: string; support value is one of: "a", "v", "d", "s" * Ratio Type for a ratio value, such as video avg frame rate * constructor(a,b) Ratio constructor Arguments: * a Dividend value * b Divisor value * Ratio.ParseFromString(string_ratio, separator) Transform a string format ration to Ratio instance Arguments: * string_ratio String ratio * separator Separator in ratio string, optional argument, default: "/" and ":" Return: If parse successfull return the Ratio instance, otherwise return null Sample: ```javascript const str = '30000/1001'; const ratio = Ratio.ParseFromString(str); console.log(ratio) // Ratio { a: 30000, b: 1001 } ``` * toNumber(fixed) Transform a Ratio instance to a float number Arguments: * fixed Result to fixed decimal places, optional argument, Default: 2 Sample: ```javascript const str = '24000/1001'; const ratio = Ratio.ParseFromString(str); console.log(ratio.toFloat()) // 23.98 console.log(ratio.toFloat(3)) // 23.976 ``` * toString(separator) Transform a Ratio instance to string Arguments: * separator Ratio separator in result Sample: ```javascript const str = '24000/1001'; const ratio = Ratio.ParseFromString(str); console.log(ratio.toString()) // "24000/1001" console.log(ratio.toString(":")) // "24000:1001" ```