Stream video from ffmpeg to webrtc

ffmpeg-to-webrtc

ffmpeg-to-webrtc demonstrates how to send video from ffmpeg to your browser using pion.

This example has the same structure as play-from-disk-h264 but instead of reading from a file it reads H264 stream from ffmpeg stdout pipe.

Instructions

Open example page

jsfiddle.net you should see two text-areas and a 'Start Session' button

Copy browser's SessionDescription

In the jsfiddle the top textarea is your browser's SDP, copy that and:

Windows

  1. Paste the SessionDescription into a file SDP.
  2. Make sure ffmpeg in your PATH.
  3. Run go run . - < SDP
  4. Note dash after ffmpeg options. It makes ffmpeg to write output to stdout. The app will read h264 stream from ffmpeg stdout.
  5. ffmpeg output format should be h264. Browsers don't support all h264 profiles so it may not always work. Here is an example of format that works: -pix_fmt yuv420p -c:v libx264 -bsf:v h264_mp4toannexb -b:v 2M -max_delay 0 -bf 0 -f h264.

Input SessionDescription from ffmpeg-to-webrtc into your browser

When you see SDP in base64 format printed it means that SDP is already in copy buffer. So you can go to jsfiddle page and paste that into second text area

Hit 'Start Session' in jsfiddle

A video should start playing in your browser below the input boxes.

Examples (windows)

Share camera stream

go run . -rtbufsize 100M -f dshow -i video="PUT_DEVICE_NAME" -pix_fmt yuv420p -c:v libx264 -bsf:v h264_mp4toannexb -b:v 2M -max_delay 0 -bf 0 -f h264 - < SDP. There is a delay of several seconds. Should be possible to fix it with better ffmpeg configuration.

To check list of devices: ffmpeg -list_devices true -f dshow -i dummy.
It is possible also to set a resolution and a format, for example -pixel_format yuyv422 -s 640x480. Possible formats: ffmpeg -list_options true -f dshow -i video=PUT_DEVICE_NAME.

Share screen or window

See .bat files in src folder

Owner
Artur Shellunts
Senior Software Engineer
Artur Shellunts
Comments
  • All video frames parsed and sent

    All video frames parsed and sent

    I'm trying to use this to make a web remote desktop client, and ~~when using -preset ultrafast -tune zerolatency~~ (edit: not sure this is the issue) I just get All video frames parsed and sent

  • Can you share a IP camera stream without reencoding with that?

    Can you share a IP camera stream without reencoding with that?

    As in the title.

    I once looked for a solution to push h.264 stream from IP camera without reencoding via WebRTC and failed completely. I don't remember the details, but it got stuck in my head that every solution required re-encoding.

    Reencoding multiple 1080p streams would kill the server and I'd like to push several streams via WebRTC at the same time.

    Sorry if my question is incomplete at some point - I'm definitely a beginner when it comes to streaming.

  • How to stream rtsp streams with copy codec to webrtc.

    How to stream rtsp streams with copy codec to webrtc.

    I am trying to stream my cctv streams to webrtc i have alredy tested many commands none of them works. It returns "All video frames parsed and sent" Please help me. I am using windows planning to deploy on linux.

  • Receive audio and send to ffmpeg

    Receive audio and send to ffmpeg

    Hello how are you?

    Now I need to receive the sound from the browser microphone, this code receives the RTP Track and sends it via STDIN to ffmpeg, but I don't hear any sound, what am I doing wrong

    package main
    
    import (
    	"fmt"
    	"time"
    	"os"
        "os/exec"
    
    	"github.com/pion/rtcp"
    	"github.com/pion/webrtc/v3"
    	//gst "gstreamer-sink"
    )
    
    func check(err error) {
    	if err != nil {
    		panic(err)
    	}
    }
    
    func main() {
    	// Prepare the configuration
    	config := webrtc.Configuration{
    		ICEServers: []webrtc.ICEServer{
    			{
    				URLs: []string{"stun:stun.l.google.com:19302"},
    			},
    		},
    	}
    
    	// Create a new RTCPeerConnection
    	peerConnection, err := webrtc.NewPeerConnection(config)
    	if err != nil {
    		panic(err)
    	}
    
    	// Set a handler for when a new remote track starts, this handler creates a gstreamer pipeline
    	// for the given codec
    	peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
    
    		// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
    		go func() {
    			ticker := time.NewTicker(time.Second * 3)
    			for range ticker.C {
    				rtcpSendErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
    				if rtcpSendErr != nil {
    					fmt.Println(rtcpSendErr)
    				}				
    			}
    		}()
    
    		//codecName := strings.Split(track.Codec().RTPCodecCapability.MimeType, "/")[1]
    		//fmt.Printf("Track has started, of type %d: %s \n", track.PayloadType(), codecName)
    		//pipeline := gst.CreatePipeline(track.PayloadType(), strings.ToLower(codecName))
    		//pipeline.Start()
    		
    		buf := make([]byte, 1400)
    		chBuff := make(chan []byte, 1400)
    
    		go playTrack(chBuff)
    
    		for {
    			i, _, readErr := track.Read(buf)
    			if readErr != nil {
    				panic(err)
    			}
    			chBuff <- buf[:i]
    			//pipeline.Push(buf[:i])
    			//fmt.Printf("%x", buf[:i])
    			//fmt.Println(track.PayloadType())			
    		}
    	})
    
    	// Set the handler for ICE connection state
    	// This will notify you when the peer has connected/disconnected
    	peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
    		fmt.Printf("Connection State has changed %s \n", connectionState.String())
    	})
    
    	// Wait for the offer to be pasted
    	offer := webrtc.SessionDescription{}
    	Decode(MustReadStdin(), &offer)
    
    	// Set the remote SessionDescription
    	err = peerConnection.SetRemoteDescription(offer)
    	if err != nil {
    		panic(err)
    	}
    
    	// Create an answer
    	answer, err := peerConnection.CreateAnswer(nil)
    	if err != nil {
    		panic(err)
    	}
    
    	// Create channel that is blocked until ICE Gathering is complete
    	gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
    
    	// Sets the LocalDescription, and starts our UDP listeners
    	err = peerConnection.SetLocalDescription(answer)
    	if err != nil {
    		panic(err)
    	}
    
    	// Block until ICE Gathering is complete, disabling trickle ICE
    	// we do this because we only can exchange one signaling message
    	// in a production application you should exchange ICE Candidates via OnICECandidate
    	<-gatherComplete
    
    	// Output the answer in base64 so we can paste it in browser
    	fmt.Println(Encode(*peerConnection.LocalDescription()))
    
    	// Block forever
    	select {}
    }
    
    func playTrack(ch <-chan []byte){
    	//cmd := exec.Command("ffmpeg", "-i", "pipe:0", "-f", "alsa", "default")
    	cmd:= exec.Command("ffmpeg", "-i", "pipe:0", "-c:a", "copy", "-sample_fmt", "s16p", "-ssrc", "1", "-payload_type", "111",  "-b", "96k", "-f", "alsa", "default")
    
        cmd.Stderr = os.Stderr // bind log stream to stderr
        //cmd.Stdout = resultBuffer // stdout result will be written here
    
        stdin, err := cmd.StdinPipe() // Open stdin pipe
        check(err)
    
        err = cmd.Start() // Start a process on another goroutine
        check(err)
    
    	for {
        	_, err = stdin.Write(<-ch) // pump audio data to stdin pipe
        	check(err)
    	}
    
        err = stdin.Close() // close the stdin, or ffmpeg will wait forever
        check(err)
    
        err = cmd.Wait() // wait until ffmpeg finish
        check(err)
    }
    
  • "Put SDP from ffmpeg-to-webrtc into your browser"

    Hi, where do I find the SDP from ffmpeg-to-webrtc?

    I assume it's meant to appear in the commandline? Nothing seems to appear after such as command is executed; go run . -rtbufsize 100M -i test.mp4 -pix_fmt yuv420p -c:v libx264 -bsf:v h264_mp4toannexb -b:v 2M -max_delay 0 -bf 0 -f h264 -

    I added ffmpeg.exe into the src directory. Thanks!

[WIP] a very simple, tiny and intuitive ffmpeg wrapper with a cli interface for inspecting & transforming media files supported by the original ffmpeg software

About a very simple, tiny and intuitive ffmpeg wrapper with a cli interface for inspecting & transforming media files supported by the original ffmpeg

Oct 21, 2022
golang function that download a video from youtube, and convert it to a mp3 file using ffmpeg

echedwnmp3 echedwnmp3 is a function that download a video from youtube, and convert it to a mp3 file using ffmpeg example package main import(echedwn

Dec 7, 2021
lmmp3 is a little golang library that download a video from youtube, and convert it to a mp3 file using ffmpeg

lmmp3 lmmp3 is a function that download a video from youtube, and convert it to a mp3 file using ffmpeg You need to have installed ffmpeg in your syst

Aug 12, 2022
ffcommander - An easy frontend to FFmpeg and Imagemagick to automatically process video and manipulate subtitles.

% FFCOMMANDER(1) ffcommander 2.39 % Mikael Hartzell (C) 2018 % 2021 Name ffcommander - An easy frontend to FFmpeg and Imagemagick to automatically pro

May 9, 2022
A go program that relies on back-end ffmpeg to process video-related content

Video Compress A go program that relies on back-end ffmpeg to process video-related content Installation v-go You can download the corresponding v-go

Dec 22, 2021
LiveKit - Open source, distributed video/audio rooms over WebRTC

LiveKit is an open source project that provides scalable, multi-user conferencing over WebRTC. It's designed to give you everything you need to build real time video/audio capabilities in your applications.

Jan 9, 2023
rtsp to webrtc proxy with websocket signaling, currently limited to single h264 stream per endpoint

rtp-to-webrtc rtp-to-webrtc demonstrates how to consume a RTP stream video UDP, and then send to a WebRTC client. With this example we have pre-made G

Aug 6, 2022
Take control over your live stream video by running it yourself. Streaming + chat out of the box.
Take control over your live stream video by running it yourself.  Streaming + chat out of the box.

Take control over your content and stream it yourself. Explore the docs » View Demo · Use Our Server for Testing · FAQ · Report Bug Table of Contents

Jan 1, 2023
Golang bindings for FFmpeg

goav Golang binding for FFmpeg A comprehensive binding to the ffmpeg video/audio manipulation library. Usage import "github.com/giorgisio/goav/avforma

Dec 27, 2022
Live on-demand transcoding in go using ffmpeg. Also with NVIDIA GPU hardware acceleration.

Go live HTTP on-demand transcoding Transcoding is expensive and resource consuming operation on CPU and GPU. For big companies with thousands of custo

Dec 16, 2022
A Go implementation of fluent-ffmpeg

A Go implementation of fluent-ffmpeg

Dec 7, 2022
A small program in Go that efficiently compresses videos using ffmpeg.

discordcompressor A small program in Go that efficiently compresses videos using ffmpeg. Dependencies FFmpeg including FFprobe Usage discordcompressor

Dec 18, 2022
Videncode - Media Encoder (with ffmpeg)

Videncode - Media Encoder (with ffmpeg) Powered by yellyoshua (With2 easy steps) - Build JSON with folder of videos > Process the videos to the new fo

Nov 19, 2022
ffmpeg core for golang

go-ffmpeg-core ffmpeg core for golang 基于ffmpeg命令封装简单功能,因此运行环境需事先安装有ffmpeg命令。 ffmpeg官方下载地址: http://ffmpeg.org/download.html 1. 剥离视频文件的音频/视频 package mai

Nov 26, 2021
Personal video streaming server.

tube This is a Golang project to build a self hosted "tube"-style video player for watching your own video collection over HTTP or hosting your own ch

Jan 5, 2023
Short video direct link acquisition 短视频直连获取工具
Short video direct link acquisition 短视频直连获取工具

Glink 短视频去水印一键解析应用 Short video direct link acquisition 短视频直连获取工具 Glink是一款基于go语言开发的短视频解析应用,前端使用vue+argon主题,后端使用go-fiber框架,支持web在线模式、客户端模式。

Dec 7, 2022
Quik.do is a video conferencing tool.
Quik.do is a video conferencing tool.

Quik.do is a video conferencing tool.

Jan 3, 2023
live video streaming server in golang
live video streaming server in golang

中文 Simple and efficient live broadcast server: Very simple to install and use; Pure Golang, high performance, and cross-platform; Supports commonly us

Jan 4, 2023
Go4vl is Go library for working with the Video for Linux API (V4L2) natively, without any C bindings.

go4vl A Go library for working with the Video for Linux user API (V4L2). Gov4l hides all the complexities of working with V4L2 and exposes idiomatic G

Dec 23, 2022