Stream video from ffmpeg to webrtc

Overview

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

Issues
  • 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

    opened by Heath123 12
  • 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.

    opened by dzek69 3
  • 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.

    opened by anand-010 2
  • 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)
    }
    
    opened by projetoarduino 2
  • "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!

    opened by ZeoWorks 1
Owner
Artur Shellunts
Senior Software Engineer
Artur Shellunts
[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

Mohammed Al Ashaal 9 Jun 28, 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

pai 4 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

pai 9 Aug 1, 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

Mikael Hartzell 2 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

JokerHeyra 0 Dec 22, 2021
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

Game On 4 May 12, 2022
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.

LiveKit 3.7k Aug 1, 2022
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

Owncast 6.2k Aug 8, 2022
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

H. Giorgis 1.9k Aug 3, 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

Miroslav Šedivý 94 Jul 31, 2022
A Go implementation of fluent-ffmpeg

A Go implementation of fluent-ffmpeg

Modfy 310 Jul 29, 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

null 25 Aug 1, 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

Yoshua Lopez 2 Oct 25, 2021
ffmpeg core for golang

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

Resynz 0 Nov 26, 2021
🔥 Golang live stream lib/client/server. support RTMP/RTSP/HLS/HTTP[S]-FLV/HTTP-TS, H264/H265/AAC, relay, cluster, record, HTTP API/Notify, GOP cache. 官方文档见 https://pengrl.com/lal

lal是一个开源GoLang直播流媒体网络传输项目,包含三个主要组成部分: lalserver:流媒体转发服务器。类似于nginx-rtmp-module等应用,但支持更多的协议,提供更丰富的功能。lalserver简介 demo:一些小应用,比如推、拉流客户端,压测工具,流分析工具,调度示例程序等

yoko 1.6k Aug 2, 2022
A tool to stream videos📺 directly into VLC media player just by its name from terminal.

PeerWatch A tool to stream videos directly into the VLC media player. Download Download the tool from here: Windows Linux win-x64 linux-x64 Building Y

Ab 0 Feb 12, 2022
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

davy wybiral 228 Jul 28, 2022
Short video direct link acquisition 短视频直连获取工具

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

佰阅 130 Aug 4, 2022
Quik.do is a video conferencing tool.

Quik.do is a video conferencing tool.

Bora Tanrıkulu 113 Jul 16, 2022