gmp4

package module
v0.0.0-...-17162cf Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2021 License: MIT Imports: 10 Imported by: 0

README

说明

gmp4

gmp4是一个能够通过视频的url\本地路径获取其时长的轻量级库,目前仅支持MP4格式的视频

安装

go get -u github.com/Stitch-Zhang/gmp4

使用

获取网络资源中MP4视频时长

https://www.runoob.com/try/demo_source/movie.mp4

Example
package main

import (
	"fmt"
	"github.com/Stitch-Zhang/gmp4"
)

func main() {
	video, err := gmp4.NewRemote("https://www.runoob.com/try/demo_source/movie.mp4")
	if err != nil {
		fmt.Println("获取失败:", err)
		return
	}
	videoTime := gmp4.GetDuration(video)
	fmt.Printf("获取成功,时长=%ds", videoTime)
}

///获取成功,时长=:12s
设置请求头
···
    video, err := gmp4.NewRemote("https://www.runoob.com/try/demo_source/movie.mp4")
	if err != nil {
		fmt.Println("获取失败:", err)
		return
    }
    header :=http.Header{
	    "User-Agent": {"Mozilla/5.0 (Windows NT 10.0; x64; rv:85.0) Gecko/20100101 Firefox/85.0"},
		///Key,Value 有两种方式存放
		///方法1
		"Cookie": {"cookie1_key=cookie1_value","cookie2_key=cookie2_value"},
		///方法2
		"Cookie": {"cookie1_key=cookie1_value;cookie2_key=cookie2_value"}
	}
	video.SetHeader(header)
···
设置请求超时
···
    video, err := gmp4.NewRemote("https://www.runoob.com/try/demo_source/movie.mp4")
	if err != nil {
		fmt.Println("获取失败:", err)
		return
    }
    video.SetTimeout(3*time.Second)

···
获取本地MP4视频时长

文件路径:C:\demo.mp4

package main

import (
	"fmt"
	"github.com/Stitch-Zhang/gmp4"
)

func main() {
	video, err := gmp4.NewLocal(`C:\demo.mp4`)
	if err != nil {
		fmt.Println("获取失败:", err)
		return
	}
	videoTime := gmp4.GetDuration(video)
	fmt.Printf("获取成功,时长=%ds", videoTime)
}

///获取成功,时长=:2233s
🎉🎉🎉

如有Bug或疑难,欢迎提交PR

Readme

gmp4

Install

go get -u github.com/Stitch-Zhang/gmp4

Usage

Get a web video duration with url

https://www.runoob.com/try/demo_source/movie.mp4

Example
package main

import (
	"fmt"
	"github.com/Stitch-Zhang/gmp4"
)

func main() {
	video, err := gmp4.NewRemote("https://www.runoob.com/try/demo_source/movie.mp4")
	if err != nil {
		fmt.Println("Failed to parse", err)
		return
	}
	videoTime := gmp4.GetDuration(video)
	fmt.Printf("Succeess!,Duration=%ds", videoTime)
}

///Succeess!,Duration=:2233s
Set request header
···
    video, err := gmp4.NewRemote("https://www.runoob.com/try/demo_source/movie.mp4")
	if err != nil {
		fmt.Println(":", err)
		return
    }
    header :=http.Header{
	    "User-Agent": {"Mozilla/5.0 (Windows NT 10.0; x64; rv:85.0) Gecko/20100101 Firefox/85.0"},
		///There are two ways to store key,value
		///Way1
		"Cookie": {"cookie1_key=cookie1_value","cookie2_key=cookie2_value"},
		///Way2
		"Cookie": {"cookie1_key=cookie1_value;cookie2_key=cookie2_value"}
	}
	video.SetHeader(header)
···
Set request timeout
···
    video, err := gmp4.NewRemote("https://www.runoob.com/try/demo_source/movie.mp4")
	if err != nil {
		fmt.Println("Failed to parse:", err)
		return
    }
    video.SetTimeout(3*time.Second)

···
Get local file mp4 duration

File path:C:\demo.mp4

package main

import (
	"fmt"
	"github.com/Stitch-Zhang/gmp4"
)

func main() {
	video, err := gmp4.NewLocal(`C:\demo.mp4`)
	if err != nil {
		fmt.Println("Failed to parse:", err)
		return
	}
	videoTime := gmp4.GetDuration(video)
	fmt.Printf("Succeess!,Duration=%ds", videoTime)
}

///Succeess!,Duration=:2233s
🎉🎉🎉

It is welcome if you can make this project better in Pull Request

Documentation

Overview

This package was written based on ISO/IEC 14496 Gain video time,only supported mp4 now

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDuration

func GetDuration(m video) uint32

GetDuration get video time(second) by downloading mvhd box of video and parse it

Types

type LocalVideo

type LocalVideo struct {
	// contains filtered or unexported fields
}

LocalVideo define a local video

func NewLocal

func NewLocal(fileName string) (*LocalVideo, error)

NewLocal return a LocalVideo

type RemoteVideo

type RemoteVideo struct {
	// contains filtered or unexported fields
}

RemoteVideo define a web video

func NewRemote

func NewRemote(rawurl string) (*RemoteVideo, error)

NewRemote return a RemoteVideo object

func (*RemoteVideo) SetHeader

func (r *RemoteVideo) SetHeader(h http.Header)

SetHeader can manually set webClient cookie UA etc

func (*RemoteVideo) SetTimeout

func (r *RemoteVideo) SetTimeout(time time.Duration)

SetTimeout can manually set webClient timeout

Jump to

Keyboard shortcuts

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