In [1]:
import requests
import cv2
import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline


/Users/baart_f/.virtualenvs/main/lib/python2.7/site-packages/matplotlib/__init__.py:872: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
  warnings.warn(self.msg_depr % (key, alt_key))

In [2]:
# surfline provides a convenient api that can be used to extract camera info
resp = requests.get('http://api.surfline.com/v1/cams/5038')

In [3]:
# the data is returned as json
data = resp.json()

In [4]:
#get the url
url = data['streamInfo']['stream'][0]['file']
url


Out[4]:
u'http://livestream.cdn-surfline.com/cdn-live/_definst_/surfline/secure/live/wc-jalamacam.smil/playlist.m3u8?e=1454621380&h=8d46ee4cddbc580bfb1052981db15556'

In [5]:
# let's record 60 seconds
!ffmpeg -y -t 60 -i "$url" test.mp4


ffmpeg version 2.8.5 Copyright (c) 2000-2016 the FFmpeg developers
  built with Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
  configuration: --prefix=/opt/local --enable-swscale --enable-avfilter --enable-avresample --enable-libmp3lame --enable-libvorbis --enable-libopus --enable-libtheora --enable-libschroedinger --enable-libopenjpeg --enable-libmodplug --enable-libvpx --enable-libsoxr --enable-libspeex --enable-libass --enable-libbluray --enable-lzma --enable-gnutls --enable-fontconfig --enable-libfreetype --enable-libfribidi --disable-indev=jack --disable-outdev=xv --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/clang --enable-vda --enable-videotoolbox --arch=x86_64 --enable-yasm --enable-gpl --enable-postproc --enable-libx264 --enable-libxvid
  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
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
[http @ 0x7fc1a9d00780] No trailing CRLF found in HTTP header.
Input #0, hls,applehttp, from 'http://livestream.cdn-surfline.com/cdn-live/_definst_/surfline/secure/live/wc-jalamacam.smil/playlist.m3u8?e=1454621380&h=8d46ee4cddbc580bfb1052981db15556':
  Duration: N/A, start: 2450.949000, bitrate: N/A
  Program 0 
    Metadata:
      variant_bitrate : 880000
    Stream #0:0: Data: timed_id3 (ID3  / 0x20334449)
    Metadata:
      variant_bitrate : 880000
    Stream #0:1: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709), 640x480 [SAR 1:1 DAR 4:3], 12 fps, 100 tbr, 90k tbn, 24 tbc
    Metadata:
      variant_bitrate : 880000
[libx264 @ 0x7fc1aa803a00] using SAR=1/1
[libx264 @ 0x7fc1aa803a00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX AVX2 FMA3 LZCNT BMI2
[libx264 @ 0x7fc1aa803a00] profile High, level 2.2
[libx264 @ 0x7fc1aa803a00] 264 - core 142 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=12 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'test.mp4':
  Metadata:
    encoder         : Lavf56.40.101
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 640x480 [SAR 1:1 DAR 4:3], q=-1--1, 12 fps, 12288 tbn, 12 tbc
    Metadata:
      variant_bitrate : 880000
      encoder         : Lavc56.60.100 libx264
Stream mapping:
  Stream #0:1 -> #0:0 (h264 (native) -> h264 (libx264))
Press [q] to stop, [?] for help
skipping 2 segments ahead, expired from playlists
[http @ 0x7fc1a9d00440] No trailing CRLF found in HTTP header.
[http @ 0x7fc1aba00c60] No trailing CRLF found in HTTP header.
[http @ 0x7fc1a9d0a4e0] No trailing CRLF found in HTTP header.
[http @ 0x7fc1a9d0a4c0] No trailing CRLF found in HTTP header.
[http @ 0x7fc1a9d00460] No trailing CRLF found in HTTP header.
[http @ 0x7fc1a9e03cc0] No trailing CRLF found in HTTP header.
[http @ 0x7fc1ab900480] No trailing CRLF found in HTTP header.
[http @ 0x7fc1a9e04880] No trailing CRLF found in HTTP header.
[http @ 0x7fc1a9d06bc0] No trailing CRLF found in HTTP header.
[http @ 0x7fc1a9e00cc0] No trailing CRLF found in HTTP header.
frame=  720 fps= 17 q=-1.0 Lsize=    5035kB time=00:00:59.83 bitrate= 689.3kbits/s dup=120 drop=0    
video:5027kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.152765%
[libx264 @ 0x7fc1aa803a00] frame I:6     Avg QP:18.44  size: 44326
[libx264 @ 0x7fc1aa803a00] frame P:469   Avg QP:20.75  size:  9326
[libx264 @ 0x7fc1aa803a00] frame B:245   Avg QP:20.05  size:  2070
[libx264 @ 0x7fc1aa803a00] consecutive B-frames: 40.3% 42.8%  0.8% 16.1%
[libx264 @ 0x7fc1aa803a00] mb I  I16..4: 33.9% 14.4% 51.8%
[libx264 @ 0x7fc1aa803a00] mb P  I16..4:  1.7%  1.7%  2.6%  P16..4: 26.3%  4.6% 10.2%  0.0%  0.0%    skip:53.0%
[libx264 @ 0x7fc1aa803a00] mb B  I16..4:  0.1%  0.0%  0.0%  B16..8: 21.2%  3.4%  4.6%  direct: 2.2%  skip:68.4%  L0:53.6% L1:40.5% BI: 5.9%
[libx264 @ 0x7fc1aa803a00] 8x8 transform intra:25.8% inter:16.9%
[libx264 @ 0x7fc1aa803a00] coded y,uvDC,uvAC intra: 79.1% 28.3% 3.5% inter: 15.4% 3.3% 0.0%
[libx264 @ 0x7fc1aa803a00] i16 v,h,dc,p:  6% 55% 27% 12%
[libx264 @ 0x7fc1aa803a00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu:  8% 53% 21%  2%  1%  1%  4%  1%  8%
[libx264 @ 0x7fc1aa803a00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu:  7% 54% 16%  3%  2%  1%  5%  2%  9%
[libx264 @ 0x7fc1aa803a00] i8c dc,h,v,p: 45% 50%  3%  2%
[libx264 @ 0x7fc1aa803a00] Weighted P-Frames: Y:3.6% UV:0.4%
[libx264 @ 0x7fc1aa803a00] ref P L0: 86.9% 10.1%  2.5%  0.5%  0.0%
[libx264 @ 0x7fc1aa803a00] ref B L0: 98.8%  1.2%  0.0%
[libx264 @ 0x7fc1aa803a00] ref B L1: 99.9%  0.1%
[libx264 @ 0x7fc1aa803a00] kb/s:686.28

In [6]:
# open the stream as an opencv device
stream = cv2.VideoCapture('test.mp4')

In [7]:
# we should have some video now, 20s @ 12 fps
!ffprobe test.mp4


ffprobe version 2.8.5 Copyright (c) 2007-2016 the FFmpeg developers
  built with Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
  configuration: --prefix=/opt/local --enable-swscale --enable-avfilter --enable-avresample --enable-libmp3lame --enable-libvorbis --enable-libopus --enable-libtheora --enable-libschroedinger --enable-libopenjpeg --enable-libmodplug --enable-libvpx --enable-libsoxr --enable-libspeex --enable-libass --enable-libbluray --enable-lzma --enable-gnutls --enable-fontconfig --enable-libfreetype --enable-libfribidi --disable-indev=jack --disable-outdev=xv --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/clang --enable-vda --enable-videotoolbox --arch=x86_64 --enable-yasm --enable-gpl --enable-postproc --enable-libx264 --enable-libxvid
  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
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.40.101
  Duration: 00:01:00.00, start: 0.000000, bitrate: 687 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 686 kb/s, 12 fps, 12 tbr, 12288 tbn, 24 tbc (default)
    Metadata:
      handler_name    : VideoHandler

In [8]:
# read the first frame
status, img = stream.read()
# create an array of the same shape
sum = np.zeros_like(img, dtype='float32')
# we're reading 200 frames as an example
n = 700
for i in range(n):
    # read the img
    status, img = stream.read()
    # keep a running mean
    cv2.accumulateWeighted(img, sum, 1.0/n)

In [10]:
# show the mean intensity 
fig, axes = plt.subplots(1, 2, figsize=(13,8))
axes[0].imshow(cv2.cvtColor(cv2.convertScaleAbs(sum), cv2.COLOR_BGR2GRAY), cmap='viridis')
axes[1].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))


Out[10]:
<matplotlib.image.AxesImage at 0x10c4ee690>

In [ ]:


In [ ]: