# ----------------------------------------------------------------------------- # DEB-MULTIMEDIA DEPOSU # ----------------------------------------------------------------------------- # /etc/apt/sources.list Multimedia ve wheezy-backports depoları eklenecek. deb http://ftp.us.debian.org/debian/ wheezy main non-free contrib deb-src http://ftp.us.debian.org/debian/ wheezy main non-free contrib deb http://security.debian.org/ wheezy/updates main non-free contrib deb-src http://security.debian.org/ wheezy/updates main non-free contrib deb http://www.deb-multimedia.org wheezy main non-free deb-src http://www.deb-multimedia.org wheezy main non-free deb http://http.debian.net/debian wheezy-backports main deb-src http://http.debian.net/debian wheezy-backports main deb http://www.deb-multimedia.org wheezy-backports main deb-src http://www.deb-multimedia.org wheezy-backports main # Keyring aptitude update aptitude install deb-multimedia-keyring aptitude update # ----------------------------------------------------------------------------- # FFMPEG # ----------------------------------------------------------------------------- # Kurulum aptitude install -t wheezy-backports ffmpeg # İzleme ffplay -i test.avi # Sadece ses kanalını izleme ffplay -f alsa -ac 2 -i hw:0,0 ffplay -f alsa -ac 2 -i default # Webcam izleme ffplay -loglevel quiet -v quiet \ -f video4linux2 -video_size 320x240 -vb 1024k -i /dev/video0 # Ses çevrimi sırasında native AAC kullanılacaksa... -c:a libfdk_aac -vbr 1 \ yerine -strict experimental -c:a aac -ac 2 -r:a 48000 -b:a 64k \ # RTMP overlay ffmpeg -i rtmp://85.111.24.138:1935/tv/trtmuzik \ -i rtmp://85.111.24.138:1935/tv/trthd \ -filter_complex "[1:0]scale=trunc(iw/3):-1,pad=5+iw:5+ih:3:3:orange [win]; \ [0:0][win] overlay=(main_w-overlay_w):(main_h-overlay_h)" \ -vprofile baseline -preset ultrafast \ -c:v libx264 -c:a copy \ -map 0:v -map 1:a:0 \ -f flv - \ | ffplay - # MP4 metadata bloğunu dosyanın başına taşıma. ffmpeg -i input.mp4 -c:v copy -c:a copy -movflags faststart output.mp4 # Videodan resme çevirme ffmpeg -i input.mp4 -an -r 25 -qscale:v 2 \ -ss 1.0 -t 10.0 -f image2 /tmp/output_%05d.jpg # Video dosyalarını birleştirme (ffmpeg 1.1 ve sonrası gerekiyor) find . -name "par*.flv" -printf "file '%p'\n" > liste.txt ffmpeg -f concat -i liste.txt -c:v copy -c:a copy -movflags faststart out.mp4 veya ffmpeg -f concat \ -i <(find /full/path/ -name "par*.flv" -printf "file '%p'\n") \ -c:v copy -c:a copy -movflags faststart out.mp4 veya ffmpeg -f concat -i liste.txt \ -c:v copy -c:a copy -movflags faststart out.mp4 liste.txt file '/path/video1.mp4' file '/path/video2.mp4' # 2 video yan yana ffmpeg -i video1.mp4 -i video2.mp4 \ -filter_complex "[0:v:0]pad=1280:360[bg]; [bg][1:v:0]overlay=640:0" \ out.mp4 # Video üzerine resim watermark ffmpeg -i video.mp4 -i resim.png \ -filter_complex "[0:v][1:v] overlay=20:90" out.mp4 # Kaynaklar http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide http://ffmpeg.org/trac/ffmpeg/wiki/AACEncodingGuide https://sites.google.com/site/linuxencoding/x264-ffmpeg-mapping http://sonnati.wordpress.com/2011/08/19/ffmpeg-–-the-swiss-army-knife-of-internet-streaming-–-part-iii/ # ----------------------------------------------------------------------------- # MENCODER # ----------------------------------------------------------------------------- # Kurulum aptitude install mencoder # Video capture kartından ve ses kartında yayını alıp kaydetme. #WIDTH=720 #HEIGHT=480 WIDTH=1024 HEIGHT=600 VBITRATE=$((50 * 25 * $WIDTH * $HEIGHT / 256)) mencoder -nocache -aspect 16:9 tv:// \ -tv driver=v4l2:device=/dev/video0:input=0:normid=5:\ mjpeg:decimation=1:quality=80:\ alsa:adevice=hw.0:forceaudio:audiorate=48000:amode=1:immediatemode=0 \ -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=$VBITRATE:autoaspect \ -oac copy \ -o test.avi # Yayını pipe ile aktarma Sadece son satır değişecek -o - -really-quiet | # ----------------------------------------------------------------------------- # MPLAYER && SMPLAYER # ----------------------------------------------------------------------------- mplayer2 ve smplayer2 paketleri, daha eski ffmpeg kodlarını kullandığı için performansta düşüklük görüldü. Video izlerken bazı durumlarda ffplay daha iyi netice vermekte. # Kurulum aptitude install mplayer smplayer # Raspberry Pi izleme mplayer -nocache -aspect 16:9 \ tv:// -tv driver=v4l2:device=/dev/video0:input=0:normid=0 # ----------------------------------------------------------------------------- # TVTIME # ----------------------------------------------------------------------------- # Kurulum aptitude install tvtime # Izleme tvtime # Seste sorun çıkarsa ffplay ile ses kanalını dinle ffplay -f alsa -ac 2 -i hw:0,0 # Line-in'i dinleme arecord -D hw:0 -r 44100 -c 2 -f S16_LE | aplay - arecord -D hw:0 -r 44100 -c 2 -f S32_LE | aplay - # ----------------------------------------------------------------------------- # ALSA # ----------------------------------------------------------------------------- Pulseaudio, ses ve videoyu farklı kanallardan alırken senkron sorununa sebep oluyor, yüklenmesin. # Kurulum aptitude install alsa-base alsa-utils # Ses kartından grafik kartın HDMI çıktısına sesi embed etmek için alsamixer ile ses kartı olarak HDMI seçilir. S/PDIF "M" tuşuna basılarak aktif hale getirilir. # Line in'den ses alınırken iyi sonuç veren bazı ayarlar: Playback Master -> 80 Headphone -> 80 PCM -> 80 Front -> 80 Front mic -> MM Surround -> 80 Center -> 80 LFE -> MM Line -> MM CD -> MM S/PDIF -> MM S/PDIF Defa -> MM Dynamic Pow -> Disabled Independent -> OFF Loopback Mi -> Disabled Rear Mic -> MM Smart 5.1 -> MM Capture Capture -> 0 Capture 1 -> 0 Digital -> 50 Input Source -> Line # ----------------------------------------------------------------------------- # AVIDEMUX # ----------------------------------------------------------------------------- # Kurulum aptitude install avidemux avidemux-cli aptitude install mjpegtools lame twolame # ----------------------------------------------------------------------------- # NGINX RTMP MODULE # ----------------------------------------------------------------------------- Roman Arutyunyan'ın depoda geliştirme durdu. Güncellemeleri Sergey Dryabzhinsky'nin depodan takip et. https://github.com/sergey-dryabzhinsky/nginx-rtmp-module # Derleme icin gerekli paketler. aptitude install dpkg-dev build-essential fakeroot # Paketlerin alınması (normal kullanıcı) mkdir source cd source mkdir nginx nginx_rtmp cd nginx_rtmp wget --no-check-certificate https://github.com/arut/nginx-rtmp-module/tarball/master tar zxf master cd ../nginx apt-get source -t wheezy-backports nginx-extras cp -arp ../nginx_rtmp/arut-nginx-rtmp-module-* nginx-1.6.0/debian/modules/nginx-rtmp-module # Derleme hazırlıkları - vim nginx-1.6.0/debian/rules config.status.extras ... ... # satir 112-114 --add-module=$(MODULESDIR)/nginx-upstream-fair \ --add-module=$(MODULESDIR)/nginx-rtmp-module \ --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module - build-dep su - aptitude install libexpat1-dev liblua5.1-0-dev aptitude build-dep -t wheezy-backports nginx-extras aptitude install -t wheezy-backports libavcodec-dev libavformat-dev libavutil-dev exit # Derleme cd nginx-1.6.0/ dpkg-buildpackage -rfakeroot -uc -b cd .. su aptitude install init-system-helpers dpkg -i nginx-common_1.6.0-1\~bpo70+1_all.deb dpkg -i nginx-extras_1.6.0-1\~bpo70+1_amd64.deb aptitude hold nginx-common nginx-extras # Nginx ayarları /etc/nginx/nginx.conf # worker_process degeri, core sayisi kadar olsun. worker_processes 4; rtmp_auto_push on; rtmp { server { listen 1935; drop_idle_publisher 10s; # Gstreamer ile push yapilacaksa... publish_time_fix off; application live { live on; idle_streams off; # Bir sunucudaki butun streamler cekilecekse... # pull rtmp://xxx.www.yyy.zzz:1935; # Bir sunucudaki sadece bir stream cekilecekse... # pull rtmp://xxx.www.yyy.zzz:1935/live/stream name=stream1 static; # Yayin gonderebilecek IP adresleri. allow publish 127.0.0.1; allow publish X.Y.W.Z; deny publish all; # Yayini izleyebilecek IP adresleri. allow play all; # HLS formatinda da yayin yapilacaksa... hls on; hls_path /var/tmp/hls; hls_fragment 10s; hls_playlist_length 60s; hls_sync 300ms; hls_continuous on; hls_cleanup on; # Yayina baslamak icin key-frame beklenecekse... wait_video on; wait_key on; # Yayini kaydilecekse... record all; record_path /var/tmp/record; record_unique on; record_interval 30m; record_suffix -%Y%m%d-%T.flv; # exec ile push (ffmpeg yuklu olmasi gerekiyor) allow play 127.0.0.1; exec /home/kullanici/scripts/push.sh } } } /etc/nginx/sites-available/default location /hls { alias /var/tmp/hls; } location /video { alias /var/tmp/record; autoindex on; } location /rtmp_stat { rtmp_stat all; rtmp_stat_stylesheet rtmp_stat.xsl; } location /rtmp_stat.xsl { root /var/tmp/; } location /rtmp_control { rtmp_control all; auth_basic "Restricted"; auth_basic_user_file /home/kullanici/nginx/htpasswd; } - rtmp_stat.xsl cp /home/kullanici/source/nginx/nginx-1.6.0/debian/modules/nginx-rtmp-module/stat.xsl /var/tmp/rtmp_stat.xsl chown www-data: /var/tmp/rtmp_stat.xsl RTMP durumunu takip etmek için: http://ip_adresi/rtmp_stat - rtmp_control # Bir izleyiciyi drop etme. curl -u user:passwd "http://ip_adresi/rtmp_control/drop/client?app=uygulama_adi&name=yayin_adi&addr=X.Y.W.Z" # Bütün izleyicileri drop etme. curl -u user:passwd "http://ip_adresi/rtmp_control/drop/client?app=uygulama_adi&name=yayin_adi" # Bir yayıncıyı drop etme. curl -u user:passwd "http://ip_adresi/rtmp_control/drop/publisher?app=uygulama_adi&name=yayin_adi" - tmpfs altında hls path mkdir /var/tmp/hls chown www-data: /var/tmp/hls mkdir /var/tmp/record chown www-data: /var/tmp/record - push.sh mkdir -p /home/kullanici/scripts touch /home/kullanici/scripts/push.sh chown www-data: /home/kullanici/scripts/push.sh chmod u+x /home/kullanici/scripts/push.sh vim /home/kullanici/scripts/push.sh #!/bin/bash app=$1 name=$2 ps aux | egrep ffmpeg | egrep "/$app/" | egrep "/$name " | awk '{ print $2; }' | xargs -iX kill X sleep 2 ffmpeg -i rtmp://localhost:1935/$app/$name -vcodec copy -acodec copy -f flv rtmp://X.Y.V.Z:1935/$app/$name - restart /etc/init.d/nginx configtest /etc/init.d/nginx restart - HLS klasörü ramdisk olacaksa /etc/fstab icinde tmpfs /var/tmp/hls tmpfs noatime,size=512M,nr_inodes=10k,mode=1777 0 0 - Belli aralıklarla eski video segmentleri temizlenecekse /root/scripts/cleanup.sh #!/bin/bash find /var/tmp/hls/ -type f -name "*.ts" -cmin +5 -delete /etc/crontab * * * * * root /root/scripts/cleanup.sh >/dev/null 2>&1 chmod u+x /root/scripts/cleanup.sh # ----------------------------------------------------------------------------- # Stream gönderme # ----------------------------------------------------------------------------- ffmpeg -i mmsh://85.111.3.55/ntv \ -threads 0 -vprofile baseline -preset medium \ -c:v libx264 -b:v 256k -maxrate 256k -bufsize 512k -g 1 \ -c:a libfdk_aac -vbr 1 \ -f flv rtmp://localhost:1935/live/yayin1 # İzleme ffplay rtmp://localhost:1935/live/yayin1 ffplay http://localhost/hls/yayin1.m3u8 # Kaynaklar https://github.com/arut/nginx-rtmp-module http://rarut.wordpress.com/ # ----------------------------------------------------------------------------- # KOMUTLARIN BİRLİKTE KULLANIMI # ----------------------------------------------------------------------------- # Video ve ses kanallarını alıp RTMP sunucuya gönderme #!/bin/bash ASPECT="16:9" WIDTH=1280 HEIGHT=600 VBITRATE=$((50 * 25 * $WIDTH * $HEIGHT / 256)) DEVICE="/dev/video0" NORMID=5 mencoder -nocache -aspect $ASPECT tv:// \ -tv driver=v4l2:device=$DEVICE:input=0:normid=$NORMID:\ mjpeg:decimation=1:quality=60:\ alsa:adevice=hw.0:forceaudio:audiorate=48000:amode=1:immediatemode=0 \ -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=$VBITRATE:autoaspect \ -oac copy \ -o - -really-quiet | \ ffmpeg -re -i - -threads 0 \ -c:v libx264 -profile:v baseline -preset slow \ -b:v 512k -maxrate 512k -bufsize 1024k -aspect $ASPECT -g 1 \ -c:a libfdk_aac -vbr 1 \ -f flv rtmp://localhost:1935/live/yayin1 # ffplay ile pipe üzerinden izleme local'de izleme #!/bin/bash ASPECT="16:9" WIDTH=1920 HEIGHT=1080 VBITRATE=$((50 * 25 * $WIDTH * $HEIGHT / 256)) DEVICE="/dev/video0" NORMID=5 mencoder -nocache -aspect $ASPECT tv:// \ -tv driver=v4l2:device=$DEVICE:input=0:normid=$NORMID:\ alsa:adevice=hw.0:forceaudio:audiorate=48000:amode=1:immediatemode=0 \ -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=$VBITRATE:autoaspect \ -oac copy \ -o - -really-quiet | \ ffmpeg -re -i - -threads 0 \ -c:v libx264 -profile:v baseline -preset ultrafast -g 1 \ -c:a libfdk_aac -vbr 1 \ -f flv pipe:1 | \ ffplay -f flv -i - # Sesin, mencoder tarafından işlenmesi istenmiyorsa... #!/bin/bash ASPECT="16:9" WIDTH=1920 HEIGHT=1080 VBITRATE=$((50 * 25 * $WIDTH * $HEIGHT / 256)) DEVICE="/dev/video0" NORMID=5 mencoder -nocache -aspect $ASPECT tv:// \ -tv driver=v4l2:device=$DEVICE:input=0:normid=$NORMID:\ -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=$VBITRATE:autoaspect \ -nosound \ -o - -really-quiet | \ ffmpeg -re -i - \ -f alsa -i default \ -threads 0 \ -vcodec libx264 -vprofile baseline -preset medium \ -b:v 512k -maxrate 512k -bufsize 1024k -aspect $ASPECT -g 1 \ -c:a libfdk_aac -vbr 1 \ -f flv rtmp://localhost:1935/live/yayin1 # ----------------------------------------------------------------------------- # AMAZON EC2 # ----------------------------------------------------------------------------- # Debian Wheezy makine Ireland ami-d81b12ac 379101102735/debian-wheezy-amd64-20130208 Default user: admin # ----------------------------------------------------------------------------- # NGINX İLE ŞARTLI STREAM İZLEME # ----------------------------------------------------------------------------- # Kurulumlar aptitude install python-geoip aptitude install uwsgi uwsgi-plugin-python # Python kontrol kodu /home/kullanici/wsgi/geocheck.py #!/usr/bin/python #-*- coding: utf-8 -*- import re import GeoIP from cgi import parse_qs # Bolgesel erisim denetimi aktif mi? Aktif ise sadece izinli bolgeden gelen # izleyiciler yayini izleyebilir. ENABLE_GEOIP = False # Izinli bolgeler. ALLOWED_COUNTRIES = ['TR', 'US'] # pageurl icin regex. RE_PAGEURL = re.compile('http://([a-zA-Z0-9._-]+)') # Izinli sitelerin listesi. ALLOWED_SITES = ['emrah.com', 'www.emrah.com'] def application(env, start_response): # Default olarak izleyiciye izin verilir. status = '200 OK' output = 'OK' # Izleyici bilgilerini al. args = parse_qs(env['wsgi.input'].read()) addr = args.get('addr', [''])[0] pageurl = args.get('pageurl', [''])[0] # Referer adresi al. site = '' g = RE_PAGEURL.search(pageurl) if g: site = g.group(1) # Izleyici bolge kodunu al. g = GeoIP.open('/usr/share/GeoIP/GeoIP.dat', GeoIP.GEOIP_STANDARD) country = g.country_code_by_addr(addr) # Bolge denetimi varsa ve izleyici, izin verilen bolgeden degilse, yayini # izleyemez. if (ENABLE_GEOIP is True) and (country not in ALLOWED_COUNTRIES): status = '500 ERROR' output = 'error' # Ziyaretci bir web sitesinden geldiyse ve bu site, izinli siteler # listesinde yoksa, yayini izleyemez. elif len(pageurl) and (site not in ALLOWED_SITES): status = '500 ERROR' output = 'error' # Cevabi dondur. '200 OK' donerse stream baslar, '500 ERROR' donerse stream # baslamaz. response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output] # UWSGI ayarları /etc/uwsgi/apps-available/geocheck.ini [uwsgi] plugins = python uid = www-data gid = www-data master = true socket=/var/run/uwsgi/app/geocheck/socket wsgi-file = /home/kullanici/wsgi/geocheck.py cd /etc/uwsgi/apps-enabled ln -s ../apps-available/geocheck.ini . /etc/init.d/uwsgi restart # Stream'i izlemek isteyenlerin kontrol sayfasına yönlendirilmesi /etc/nginx/nginx.conf rtmp { server { listen 1935; application live { live on; on_play http://localhost/geocheck; ... ... } /etc/nginx/sites-enabled/default location /geocheck { include uwsgi_params; uwsgi_pass unix:/var/run/uwsgi/app/geocheck/socket; } # Kaynaklar https://github.com/arut/nginx-rtmp-module/wiki/Directives https://github.com/sergey-dryabzhinsky/nginx-rtmp-module # RTMP/RTSP/MMS/M3U8 adresleri http://radyodelisi.blogspot.com/