• Stars
    star
    168
  • Rank 225,507 (Top 5 %)
  • Language
    C
  • Created over 11 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Replicate SPS/PPS in h264 bit streams

Live HLS streaming with the Raspberry Pi camera.

I wanted to use the Raspberry Pi camera to encode video for HLS streaming, the format used by iPhones, iPads and many other kinds of device. HLS uses h264 encoding for video and AAC for audio.

Unfortunately I couldn't easily discover how to modify raspivid to produce videos in the precise format required for HLS. I expect that it's possible and that I didn't look hard enough.

The problem is this: the h264 bitstream consists of a series of NAL (Network Abstraction Layer) units. Each unit has a type (a unit is similar to a packet: just a chunk of data with a header); many contain compressed video data but two, SPS and PPS contain parameters about the dimensions of the video (amongst other things) that are needed to decode it properly.

Apple's HLS HTTP streaming protocol transfers chunks of video and/or audio in chunks typically between four and twelve seconds long. Each one of these chunks has to be a self contained video which means that it has to contain SPS and PPS NALs.

By default it seems that raspivid only places SPS and PPS at the start of the stream. That means that when the stream is chopped up into chunks for HLS transfer only the first of them - which contains the SPS and PPS NALs - is playable.

psips

The problem can be fixed by passing the h264 bit stream through a filter that

  • stores any SPS and PPS NALs it sees in the stream
  • places a copy of the most recent SPS and PPS just before any IDR (key) frames.

The reason for that is that we're next going to pass the stream to ffmpeg to be split into chunks a few seconds long and it can only split the stream at a key frame. Placing SPS and PPS right before the IDR frame means that ffmpeg will split the stream just before the SPS, PPS pair - which is exactly what we want.

To build it on your Raspberry Pi:

$ sudo apt-get install git build-essential autoconf libtool
$ cd /to/some/work/dir
$ git clone git://github.com/AndyA/psips.git
$ cd psips
$ ./setup.sh && ./configure && make && sudo make install

You use it like this:

$ raspivid -w 1280 -h 720 -fps 25 -hf -t 86400000 -b 1800000 -o - | psips > live.h264

Or you could use it to add SPS and PPS for an existing file like this:

$ psips < oldfile.h264 > newfile.h264

and it works...

In the example directory there's a shell script, /examples/hls.sh, that packages HLS and makes it available for streaming via your webserver.

#!/bin/bash

base="/usr/share/nginx/www"

set -x

rm -rf live live.h264 "$base/live"
mkdir -p live
ln -s "$PWD/live" "$base/live"

# fifos seem to work more reliably than pipes - and the fact that the
# fifo can be named helps ffmpeg guess the format correctly.
mkfifo live.h264
raspivid -w 1280 -h 720 -fps 25 -hf -t 86400000 -b 1800000 -o - | psips > live.h264 &

# Letting the buffer fill a little seems to help ffmpeg to id the stream
sleep 2

# Need ffmpeg around 1.0.5 or later. The stock Debian ffmpeg won't work.
# I'm not aware of options apart from building it from source. I have
# Raspbian packages built from Debian Multimedia sources. Available on
# request but I don't want to post them publicly because I haven't cross
# compiled all of Debian Multimedia and conflicts can occur.
ffmpeg -y \
  -i live.h264 \
  -f s16le -i /dev/zero -r:a 48000 -ac 2 \
  -c:v copy \
  -c:a libfaac -b:a 128k \
  -map 0:0 -map 1:0 \
  -f segment \
  -segment_time 8 \
  -segment_format mpegts \
  -segment_list "$base/live.m3u8" \
  -segment_list_size 720 \
  -segment_list_flags live \
  -segment_list_type m3u8 \
  "live/%08d.ts" < /dev/null 

At the time of writing that's been successfully streaming live video from my Raspberry Pi camera for quite a few hours. And the quality is astonishing for such a small camera. I'll link to some once it's daylight again and I have something worth sharing.

Andy Armstrong, [email protected]

More Repositories

1

htop-osx

A port of htop to OSX
C
178
star
2

rsync

My rsync fork
C
48
star
3

ts-split

Split an MPEG2 transport stream into chunks on GOP boundaries
C
36
star
4

Test-Harness

Run Perl standard test scripts with statistics
Perl
22
star
5

CPAN--Mini--Inject

Inject modules into a CPAN::Mini mirror.
Perl
17
star
6

beebem

BBC Micro Emulator
C++
16
star
7

as3svgrendererlib

Render SVG in Flash
ActionScript
13
star
8

Geo--Hash

Encode / decode geohash.org locations.
Perl
12
star
9

timewarp

Perform basic temporal resampling on an ffmpeg yuv4mpeg2 stream
C
10
star
10

FuseDAV

fusedav is a Linux userspace file system driver for mounting WebDAV shares. It makes use of FUSE as userspace file system API and neon as WebDAV API.
C
10
star
11

CGI--Simple

A Simple totally OO CGI interface that is CGI.pm compliant
Perl
10
star
12

fdmf

Find Duplicate Music Files
C
9
star
13

HTTP--Proxy--GreaseMonkey

Run GreaseMonkey scripts in any browser
Perl
8
star
14

dotfiles

My portable home directory
Vim Script
7
star
15

ffmbc

Clone of FFmbc
C
7
star
16

Acme--6502

Pure Perl 65C02 simulator.
Perl
7
star
17

BatPhone

Bat monitoring for smart phones
C
6
star
18

abnfgen

For of abnfgen
C
6
star
19

jsonpath-faster

A faster version of jsonpath
JavaScript
5
star
20

pivid

A playground for Raspberry Pi streaming experiments
Shell
5
star
21

XML--RSS--PicLens

Create a PicLens compatible RSS feed
Perl
4
star
22

Perl--Version

Parse and manipulate Perl version strings
Perl
4
star
23

libhls

HLS M3U8 manifest handling in C
C
4
star
24

Geo--Gpx

Create and parse GPX files.
Perl
4
star
25

File--Monitor

Monitor files and directories for changes.
Perl
4
star
26

v4l2h264

Stream h264 from a v4l2 device.
C
4
star
27

gpx2svg

Convert GPX data to SVG route plot / elevation graph.
Perl
3
star
28

URI--geo

The geo URI scheme.
Perl
3
star
29

hls-client

HLS Downloader
Perl
3
star
30

isoboxer

Parse ISO MP4 structure definitions and generate code to (de)serialise them
C
3
star
31

GPS--Babel

Perl interface to gpsbabel
Perl
3
star
32

gforth-tap

TAP based testing for gforth
Forth
3
star
33

Net--CIDR--Set

Manipulate sets of IP addresses
Perl
3
star
34

perl-versions

Hacky project to build lots of perls and maintain the modules installed against them using git
Perl
3
star
35

Set--IntSpan--Fast--XS

Faster Set::IntSpan::Fast
Perl
2
star
36

Devel--LeakTrace--Fast

indicate where leaked variables are coming from.
C
2
star
37

HTML--Tiny

Lightweight, dependency free HTML/XML generation
Perl
2
star
38

Set--IntSpan--Fast

Fast handling of sets containing integer spans.
Perl
2
star
39

IPC--ShareLite

Lightweight interface to shared memory
Perl
2
star
40

TextMate--JumpTo

Tell TextMate to jump to a particular file, line
Perl
2
star
41

svgweb

Scalable Vector Graphics for Web Browsers using Flash
JavaScript
2
star
42

Remarc

BBC Remarc Project
Java
2
star
43

Data--Structure--Util

Change nature of data within a structure
Perl
2
star
44

node-bktree-fast

A fast BK tree
C
2
star
45

emitron

Emitron Streaming Media Server
TypeScript
2
star
46

grafana-loki-client

A pure JS client for Grafana Loki
JavaScript
2
star
47

WWW--TarPipe

An interface to tarpipe.com's REST based web service.
Perl
2
star
48

WWW-Notifo

Interface to notifo.com notification service
Perl
2
star
49

Digest--MD6

Perl interface to the MD6 Algorithm
C
2
star
50

Perl-Builder

Patch and build old versions of Perl
Perl
2
star
51

Devel--LeakGuard--Object

Scoped checks for object leaks
Perl
2
star
52

Test--Steering

Execute test scripts conditionally
Perl
2
star
53

re--engine--Oniguruma

Use the Oniguruma regex engine with Perl
C
2
star
54

jsondata

JSON data structures, parsing, generation in C.
C
2
star
55

Crypt--PerfectPaperPasswords

Steve Gibson's Perfect Paper Passwords
Perl
2
star
56

WWW--Plurk

Unoffical plurk.com API
PHP
1
star
57

MyPerl

My fork of the Perl repo
Perl
1
star
58

Geo--Lookup--ByTime

Lookup location by time
Perl
1
star
59

Class--Std--Slots

Provide signals and slots for standard classes.
Perl
1
star
60

Data--Find

Find data in arbitrary data structures
Perl
1
star
61

Digest--HMAC_MD6

MD6 Keyed-Hashing for Message Authentication
Perl
1
star
62

Specification-TAP

TAP specification with tests
Perl
1
star
63

shootingstick

A CouchDB clone
TypeScript
1
star
64

Test--More--Diagnostic

Conditionally add YAML diagnostics to Test::More's output
Perl
1
star
65

XML--Descent

Recursive descent XML parsing
Perl
1
star
66

rfile

Files which refer to other files
C
1
star
67

latest

Use the latest Perl features
Perl
1
star
68

Path-Class-Each

Extend Path::Class to iterate lines in a file
Perl
1
star
69

HTML-jQuery

jQuery-like selector based DOM manipulation
Perl
1
star
70

rsync-backup

(yet another) rsync based incremental backup
Shell
1
star
71

multicoder

HLS encoder / packager
C
1
star
72

hds_union

HDS / ISO media processing
Perl
1
star
73

Closer

Something like closures in C
C
1
star
74

node-sureflap

Sure Petcare API
JavaScript
1
star
75

CPAN-Mini-Tested

Create a CPAN mirror using modules that have passed tests
Perl
1
star
76

like

Declare support for an interface
Perl
1
star
77

Acme--MirrorTracer

Do nothing.
Perl
1
star
78

Lingua--Abbreviate--Hierarchy

Shorten members of verbose namespaces
Perl
1
star
79

Captcha-reCAPTCHA

Captcha::reCaptcha
Perl
1
star
80

git-andya

My git source
C
1
star
81

BeebFont

BBC Micro MODE 0, 1 and 2 fonts
CSS
1
star
82

hlsm3u8

C library for parsing and generating HLS M3U8 files.
C
1
star
83

WWW-Withings

Perl interface to Withings API
Perl
1
star
84

Devel--Unplug

Simulate the non-availability of modules
Perl
1
star
85

media-pipeline

Media processing pipelines
Perl
1
star
86

Geo--Coordinates--ITM

Convert coordinates between lat/lon and Irish Transverse Mercator
PHP
1
star
87

pilot

Autonomous Hubsan quadcopter
C
1
star
88

Devel--DTrace

Enable dtrace probes for subroutine entry, exit
Perl
1
star
89

WWW--MySociety--Gaze

An interface to MySociety.org's Gazetteer service
Perl
1
star
90

Data--Object--AutoWrap

Autogenerate accessors for R/O object data
Perl
1
star
91

auton

Autonomous robot experiment with Raspberry Pi and Arduino
C
1
star
92

garm-provider-pm2

A Garm provider for pm2 runners
JavaScript
1
star
93

SOQ

Subjectively Objective Quality
JavaScript
1
star
94

Data--YAML

Easy YAML serialisation of Perl data structures
Perl
1
star
95

Sub--Become

Syntactic sugar to allow a sub to replace itself
Perl
1
star
96

APR--HTTP--Headers--Compat

Make an APR::Table look like an HTTP::Headers
Perl
1
star
97

WWW-ErnestMarples

Interface to the ernestmarples.com UK postcode lookup API
Perl
1
star
98

js-canvas-spiral

Draw a funky spiral on an HTML 5 canvas
JavaScript
1
star
99

Devel--TraceLoad

Discover which modules a Perl program loads.
Perl
1
star
100

String--Smart

Strings that know how to escape themselves.
Perl
1
star