Go实战--golang实现MP4视频文件服务器(nareix/joy4)

栏目: Go · 发布时间: 6年前

内容简介:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangshubo1989/article/details/78053856

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangshubo1989/article/details/78053856

生命不止,继续 go go go !!!

有点忙,有点懈怠,继续。

关于golang实现的静态文件服务器之前有写过:

Go实战–golang实现静态文件服务器(文件查看,文件上传,文件下载)

正好,最近在做视频方面的东西,那么先来个简单的,实现一个提供mp4视频文件的服务器吧,并且通过浏览器访问播放。

MP4文件服务器

package main

import (
    "log"
    "net/http"
    "os"
    "time"
)

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
    video, err := os.Open("./test.mp4")
    if err != nil {
        log.Fatal(err)
    }
    defer video.Close()

    http.ServeContent(w, r, "test.mp4", time.Now(), video)
}

func main() {
    http.HandleFunc("/", ServeHTTP)
    http.ListenAndServe(":8080", nil)
}
package main

import "io/ioutil"
import "log"
import "net/http"
import "strings"

type viewHandler struct{}

func (vh *viewHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    path := r.URL.Path[1:]

    data, err := ioutil.ReadFile(string(path))

    if err != nil {
        log.Printf("Error with path %s: %v", path, err)
        w.WriteHeader(404)
        w.Write([]byte("404"))
    }

    if strings.HasSuffix(path, ".html") {
        w.Header().Add("Content-Type", "text/html")
    } else if strings.HasSuffix(path, ".mp4") {
        w.Header().Add("Content-Type", "video/mp4")
    }

    w.Write(data)
}

func main() {
    http.Handle("/", new(viewHandler))
    http.ListenAndServe(":8080", nil)
}

Go实战--golang实现MP4视频文件服务器(nareix/joy4)

nareix/joy4

Golang audio/video library and streaming server

我们先浅尝辄止,简单使用一下:

package main

import (
    "fmt"

    "github.com/nareix/joy4/av"
    "github.com/nareix/joy4/av/avutil"
    "github.com/nareix/joy4/format"
)

func init() {
    format.RegisterAll()
}

func main() {
    file, _ := avutil.Open("test.mp4")
    streams, _ := file.Streams()
    for _, stream := range streams {
        if stream.Type().IsAudio() {
            astream := stream.(av.AudioCodecData)
            fmt.Println(astream.Type(), astream.SampleRate(), astream.SampleFormat(), astream.ChannelLayout())
        } else if stream.Type().IsVideo() {
            vstream := stream.(av.VideoCodecData)
            fmt.Println(vstream.Type(), vstream.Width(), vstream.Height())
        }
    }

    file.Close()
}

输出:

AAC 48000 FLTP 1ch

H264 960 720


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

App研发录:架构设计、Crash分析和竞品技术分析

App研发录:架构设计、Crash分析和竞品技术分析

包建强 / 机械工业出版社 / 2015-10-21 / CNY 59.00

本书是作者多年App开发的经验总结,从App架构的角度,重点总结了Android应用开发中常见的实用技巧和疑难问题解决方法,为打造高质量App提供有价值的实践指导,迅速提升应用开发能力和解决疑难问题的能力。本书涉及的问题有:Android基础建设、网络底层框架设计、缓存、网络流量优化、制定编程规范、模块化拆分、Crash异常的捕获与分析、持续集成、代码混淆、App竞品技术分析、项目管理和团队建设等......一起来看看 《App研发录:架构设计、Crash分析和竞品技术分析》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试