Tutorial: HD-PVR to Twitch on Mac OS with ffmpeg

Over the christmas holidays I tried to figure out how to live stream the video from my HD-PVR to Twitch.tv on Mac OS with ffmpeg without CamTwist, without XSplit and without JTVLC. Twitch.TV is a video broadcast service where you can stream and watch your gaming content. Its main purpose is for the e-sport and speedrun community but there are more and more normal gamers streaming their content – including me since a few days.

Streaming without preview windows recording

I am not able understand why most people try to record a preview window of the HD-PVR with a desktop recording software. The HD-PVR sends a high quality 1920 × 1080 pixel MPEGTS Stream over HDMI or Component and I thought that is should be possible to process this stream directly. It seems like it is possible and it quite easy if you know how to do it.

The downside is that I don’t know any option to include a stream from a webcam and microphone. Maybe someone else is more clever than me and will provide one. The HD-PVR Capture Software will cost you $30 but it is worth any penny for people who want to record their recorded HD-PVR footage with a mac . Here we go.

Step 1 : Configuring HD-PVR Capture

Download and install HD-PVR Capture. There is also a demo version available.

With this software we will send the output signal of the HDPVR to the ffmpeg encoder using UDP.  I strongly prefer this software over Arcsoft Showbiz on Windows because of its simplicity and reliability.

HD-PVR Capture UDP Streaming Mac

In HD-PVR Capture are  two important settings: Video Encoder and Network.

Video Encoder

  • Select the preset „best“.
    Personally I think that the insane setting is unnecessary in this case because we will encode the video for web streaming.

Network

  • Uncheck the „Store video recording“ option.
    We don’t want to fill up our hard drives with tons of videos.
  • Check the „Send the captured video to network using UDP“ option.
    As the IP address use your local ip 127.0.0.1 and the port 1234.

You can verify that it works this if you press the record button, start VLC and go to file -> open network and put this into the address field: udp://@:1234. Hit the play button and you should see the output video mpegts stream of your HDPVR.

Dashboard: Live streaming Twitch with HDPVR and ffmpeg on a mac

Step 2: Setting up ffmpeg

Download and install ffmpeg for mac. Inside the zip archive you will find the the extracted file „ffmpeg“. This is the executable file and should be moved to the directory /usr/local/bin/. You can do this using Terminal with the command:

sudo mv ~/Downloads/ffmpeg /usr/local/bin/

Or copy it with the finder from Downloads to the /usr/local/bin/ folder.

Step 3: Executing the ffmpeg command

This  is the ffmpeg command with several parameters which has to be executed using the Terminal. This will grab the stream from HDPVR Capture, encode it on the fly and sent it directly to the twitch servers.

ffmpeg -f mpegts -re -i 'udp://127.0.0.1:1234?fifo_size=640000&buffer_size=640000&overrun_nonfatal=1' -vcodec libx264 -s 1280x720 -g:v 40 -threads 4 -preset medium -b:v 850k -minrate 850k -maxrate 850k -bufsize 1600k -r 30 -acodec libmp3lame -ar 44100 -b:a 64k -async 44100 -f flv "rtmp://live.justin.tv/app/live_XXXXXXX_XXXXXXX"

Replace  live_XXXXX_XXXXXXXX with your personal Twitch.tv Key and then use this command on the terminal to stream the UDP Stream from HDPVR Capture to Twitch.tv.

Here is a little explanation of the few options I think I understand:

-f 
Sets the input format to MPEGTS. Should be set automatically but I wanted to be sure.
-i 
Sets the UDP stream from HD-PVR Capture as the input file for the encoding.
?fifo_size, buffer_size and overrun_nonfatal
Parameters for the UDP stream. buffer_size was my missing piece to
get rid of the artifacts I was getting in twitch. Took me 2 days to figure this out.
-re
activates realtime mode
-vcodec
Sets the video codec to libx264. It is an open source video codec for H.264.
-s
The resolution of the encoded output format. I chose 720p because it made the most sense to me. Change this if you need to.
UPDATE: Changed it to 720p. Better quality.
-g:v
Sets the maximum keyframe interval to 2. Important for twitch.
 qscale:v 
Sets the quality scale. :v means that this is only used for video and not audio. 1 is best, 31 is worst.
UPDATE: Removed this part because -b:v overrides this setting.
-threads 
Specifies the number of CPU Threads used for encoding. I use 6 on my Core i7.
UPDATE: Removed.
-preset
There are many presets for the encoder from ultrafast, fast and medium to slow and very slow. Ultrafast is too blocky. The presets „medium“ and „fast“ seem right.
-b:v , -minrate and -maxrate
Sets the encoding bitrate to 800 kbytes with a maximum and minimum bitrate of 800 kbytes excluding audio. This simulates a Constant Bitrate (CBR) which is important for twitch. Change this if you have more bandwidth for the upstream than me (10Mbit).
-bufsize
Honestly I don’t really understand what it does. I think this is the size of the buffer for the encoding but I am not sure. I read that this is important for the CBR.
-r 
Sets the frame rate to 30 frames per second. Change this if you like.
-acodec, -ar and -b:a
Sets the LAME Audio as audio encoder with a sample rate of 44,100 Hz and 64kbytes per second for the encoding.
-f 
Sets the output format to a FLV container.
„rtmp://live.justin.tv/app/live_key“
Sets the output format to Real Time Messaging Protocol with the server address of twitch.tv or justin.tv. live_XXXX_XXXXXXXXXX has the be replace with your personal Twitch.tv API Key. You will find the key on your twitch.tv dashboard under API Key.

The result should look like this:

ffmpeg udp input and flv output

Step 4: Check the twitch dashboard

The UDP Stream from the HD-PVR Capture software is now used by the ffmpeg encoder and is directly send to the twitch servers using RTMP. I get a descent result, a stable frame rate of 30 frames, nearly no compression artifacts and a Stream Configuration Quality: Excellent message on twitch. I don’t know why there are these not-existing PPS referenced error messages showing up but it does not seem to harm the stream quality.

I developed a plugin for wordpress MarcTV Twitch Status which shows the „Live!“-Status of your Twitch account.

You can find my twitch channel here. Feel free to correct my settings and post them in the comment section below.

Video

48 Antworten auf „Tutorial: HD-PVR to Twitch on Mac OS with ffmpeg“

  1. ~/Downloads/ffmpeg -f mpegts -re -i ‚udp://127.0.0.1:1234?fifo_size=640000&buffer_size=640000&overrun_nonfatal=1‘ -vcodec libx264 -s 1280×720 -g:v 40 -threads 4 -preset medium -b:v 850k -minrate 850k -maxrate 850k -bufsize 1600k -r 30 -acodec libmp3lame -ar 44100 -b:a 64k -async 44100 -f flv
    ffmpeg version 2.2.4 Copyright (c) 2000-2014 the FFmpeg developers
    built on Jun 27 2014 09:57:37 with llvm-gcc 4.2.1 (LLVM build 2336.11.00)
    configuration: –prefix=/Volumes/Ramdisk/sw –enable-gpl –enable-pthreads –enable-version3 –enable-libspeex –enable-libvpx –disable-decoder=libvpx –enable-libmp3lame –enable-libtheora –enable-libvorbis –enable-libx264 –enable-avfilter –enable-libopencore_amrwb –enable-libopencore_amrnb –enable-filters –enable-libgsm –enable-libvidstab –enable-libx265 –arch=x86_64 –enable-runtime-cpudetect
    libavutil 52. 66.100 / 52. 66.100
    libavcodec 55. 52.102 / 55. 52.102
    libavformat 55. 33.100 / 55. 33.100
    libavdevice 55. 10.100 / 55. 10.100
    libavfilter 4. 2.100 / 4. 2.100
    libswscale 2. 5.102 / 2. 5.102
    libswresample 0. 18.100 / 0. 18.100
    libpostproc 52. 3.100 / 52. 3.100
    [udp @ 0x7f98a9d003e0] bind failed: Address already in use
    udp://127.0.0.1:1234?fifo_size=640000&buffer_size=640000&overrun_nonfatal=1: Input/output error
    Conversion failed! WHAT IM I DOING WRONG?

    1. Seems like your port :1234 is already in use.
      Port configuration for HD-PVR Capture needs to get changed. Ports for custom software should be used between 49152-65535. If not there at least over 10000 to make sure no other programm is using it.

      Whatever port you are configuring in HD-PVR Capture, you need to use it from there on.

  2. Hello Marc, I would appreciate it if you would help me. I’ve tried making the /usr/local/bin/ directory using something that someone else said worked. Although it did not work. I would always get an error when I did „sudo mv ~/Downloads/ffmpeg /usr/local/bin/“ I would say „No such file OR directory“ I am very confused and need the help. I tested the streaming using VLC and although it was delayed, it worked. I am very confused and i would need help from you Marc. Thank you!

    1. I tried it first using the terminal (mkdir), but I got the same error. Today I tried it using the Finder and it worked.

      Thanks for your tutorial. Never thought streaming could be THAT easy!

    1. No, not really. I really tried to make this happen but it seems like it’s too complicated.

      Since Google entered the game streaming scene there may be hope for better tools. Especial for the Mac since most google employees use the Mac as their working device.

  3. Hi.

    I tried this and everything seems ok, no errors but the terminal window just halts after „libpostproc 53. 3.100 / 53. 3.100“ no message after that. Any clues about whats wrong?

    ffmpeg -f mpegts -re -i ‚udp://127.0.0.1:23408?fifo_size=640000&buffer_size=640000&overrun_nonfatal=1‘ -vcodec libx264 -s 1280×720 -g:v 40 -threads 4 -preset medium -b:v 850k -minrate 850k -maxrate 850k -bufsize 1600k -r 30 -acodec libmp3lame -ar 44100 -b:a 64k -async 44100 -f flv „rtmp://live.justin.tv/app/live_xxxxxxxxxx“
    ffmpeg version 2.8 Copyright (c) 2000-2015 the FFmpeg developers
    built with llvm-gcc 4.2.1 (LLVM build 2336.11.00)
    configuration: –prefix=/Volumes/Ramdisk/sw –enable-gpl –enable-pthreads –enable-version3 –enable-libspeex –enable-libvpx –disable-decoder=libvpx –enable-libmp3lame –enable-libtheora –enable-libvorbis –enable-libx264 –enable-avfilter –enable-libopencore_amrwb –enable-libopencore_amrnb –enable-filters –enable-libgsm –enable-libvidstab –enable-libx265 –disable-doc –arch=x86_64 –enable-runtime-cpudetect
    libavutil 54. 31.100 / 54. 31.100
    libavcodec 56. 60.100 / 56. 60.100
    libavformat 56. 40.101 / 56. 40.101
    libavdevice 56. 4.100 / 56. 4.100
    libavfilter 5. 40.101 / 5. 40.101
    libswscale 3. 1.101 / 3. 1.101
    libswresample 1. 2.101 / 1. 2.101
    libpostproc 53. 3.100 / 53. 3.100

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.