mediaprobe

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2022 License: MIT Imports: 18 Imported by: 0

README

Build Status Coverage Status Go Report Card GoDoc

mediaprobe

Parsing media files using bindings for getting information about codec, bitrate, dimensions, etc.

Prerequisites

It uses github.com/rakyll/magicmime for detect mimetypes and github.com/3d0c/gmf for parsing audio and video. See these packages for installation info.

TL;DR Installing on Ubuntu

  1. You need go version 1.10 or higher
  2. You need libmagic-dev
    sudo apt-get install libmagic-dev
    
  3. You need ffmpeg libraries version 4.x or higher. You may compile it from sources, or use PPA. For example, ppa:jonathonf/ffmpeg-4
    sudo add-apt-repository ppa:jonathonf/ffmpeg-4
    sudo apt-get update
    sudo apt-get install libavcodec-dev libavdevice-dev libavfilter-dev \
                          libavformat-dev libavresample-dev libavutil-dev \
                          libpostproc-dev libswresample-dev libswscale-dev
    

Usage

See godoc examples.

Benchmark

cpu: Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz
BenchmarkParse-4                	     144	   7199375 ns/op	    2107 B/op	      45 allocs/op
BenchmarkNew-4                  	   62948	     18073 ns/op	     656 B/op	       6 allocs/op
BenchmarkInfo_CalculateMime-4   	    1254	    954855 ns/op	      72 B/op	       4 allocs/op
BenchmarkInfo_FFProbe-4         	     190	   5747085 ns/op	    1913 B/op	      33 allocs/op
BenchmarkInfo_ParseImage-4      	    2074	    583830 ns/op	    1216 B/op	      19 allocs/op

Caveats

Detecting mimetype by magic number may not be accurate enough, bindings in Go often cause unexpected memory leaks.

Documentation

Overview

Package mediaprobe provides functions for parsing media files for information, such as dimensions, codecs, duration, etc. It uses bindings to ffmpeg and libmagic.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	Name         string
	MediaType    string
	MediaSubtype string
	Size         int64
	StartTime    time.Duration
	Duration     time.Duration
	BitRate      int64
	Width        int
	Height       int
	Streams      []Stream
	// contains filtered or unexported fields
}

Info contains parsed information

func New

func New(filename string) (*Info, error)

New initialized Info and calculate file size.

Accepted filename as local path or absolute url.

func Parse

func Parse(filename string) (Info, error)

Parse file media data It determines the file type by magic bytes, and parses the media data of the video or image.

Accepted filename as local path or absolute url.

Example
package main

import (
	"log"

	"github.com/Arimeka/mediaprobe"
)

func main() {
	info, err := mediaprobe.Parse("./fixtures/video.mp4")
	if err != nil {
		log.Fatalf("Error: %s\n", err)
	}

	log.Printf("Result: %+v\n", info)
}
Output:

func (*Info) CalculateMime

func (info *Info) CalculateMime() (err error)

CalculateMime calculates mime type by magic numbers Function uses libmagic bindings using github.com/rakyll/magicmime package.

func (*Info) FFProbe

func (info *Info) FFProbe() error

FFProbe parses video or audio using ffmpeg bindings It uses github.com/3d0c/gmf package

Example
package main

import (
	"log"

	"github.com/Arimeka/mediaprobe"
)

func main() {
	info, err := mediaprobe.New("./fixtures/video.mp4")
	if err != nil {
		log.Fatalf("Error: %s\n", err)
	}

	// Skipping calculate mime-type by magic numbers and immediately parse video
	if err = info.FFProbe(); err != nil {
		log.Fatalf("Error: %s\n", err)
	}

	log.Printf("Result: %+v\n", info)
}
Output:

func (*Info) ParseImage

func (info *Info) ParseImage() error

ParseImage used for retrieve image data

Example
package main

import (
	"log"

	"github.com/Arimeka/mediaprobe"
)

func main() {
	info, err := mediaprobe.New("./fixtures/image.jpeg")
	if err != nil {
		log.Fatalf("Error: %s\n", err)
	}

	// Skipping calculate mime-type by magic numbers and immediately parse image
	if err = info.ParseImage(); err != nil {
		log.Fatalf("Error: %s\n", err)
	}

	log.Printf("Result: %+v\n", info)
}
Output:

type Stream

type Stream struct {
	ID             int
	Index          int
	MediaType      string
	Codec          string
	CodecTag       string
	CodecLongName  string
	IsExperimental bool
	Profile        string
	ColorRangeName string
	SampleFmtName  string
	Bitrate        int
	Width          int
	Height         int
	AspectRation   float64
	FrameRate      float64
	AvgFrameRate   float64
	BFrames        int
	BitsPerSample  int
	PixFmt         int
	PixFmtName     string
}

Stream contains audio/video stream information

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL