Thursday, October 16, 2014

Cross compiling (and troubleshooting) FFMPEG (with raspberry pi toolchain)

This post is just these Two   posts + troubleshooting (cflags from this blog )

Basic instructions (provided you've set up your toolchain and downloaded, untar'd everything)


configure fdk_aac and build

 CCPREFIX="/opt/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-"
CC="arm-bcm2708hardfp-linux-gnueabi-gcc"
CFLAGS="-O3 -march=armv6zk -mfpu=vfp -mfloat-abi=hard"
./configure --host=arm-linux --prefix=/home/david/built_ffmpeg/ --enable-static --disable-shared
make -j$(nproc) && make install


configure x264 and build

./configure --host=arm-linux --enable-static --cross-prefix=${CCPREFIX} --prefix=/home/david/built_ffmpeg/ --extra-cflags='-O3 -march=armv6zk -mfpu=vfp -mfloat-abi=hard' --extra-ldflags='-O3 -march=armv6zk -mfpu=vfp -mfloat-abi=hard'
make -j$(nproc) && make install

configure and build ffmpeg

(Note: I disable everything but libx264 and libfdk_aac because that's all I need)

./configure --enable-cross-compile --cross-prefix=${CCPREFIX} --arch=armel --target-os=linux --prefix=/home/david/built_ffmpeg/ \
            --disable-all \
            --enable-gpl \
            --enable-libx264 \
            --enable-nonfree \
            --enable-libfdk_aac \
            --extra-cflags="-I/home/david/built_ffmpeg/include -O3 -march=armv6zk -mfpu=vfp -mfloat-abi=hard" \
            --extra-ldflags="-L/home/david/built_ffmpeg/lib" \
            --extra-libs=-ldl
make -j$(nproc) && make install

Clearly replacing "/home/david/built_ffmpeg" for your prefix

Note: these cflags don't seem to change cpu load.




Troubleshooting:

If you are getting "No valid C compiler found", but the toolchain is in your path and on your CCPREFIX, then most likely you are missing some libraries to use it (the toolchain uses shared libraries, which you might not have)

Run:
echo "int main(){0;}" | arm-bcm2708hardfp-linux-gnueabi-gcc -x c -

To test your toolchain. It shouldn't work, and will tell you which library you are missing. Install it and try again.


If you are trying to compile ffmpeg with x264 and you've built it with --enable-static you'll likely get the error "libx264 not found" ( just like this ), even though the cflags, ldflags and prefix are all OK.
The problem is that (as you can see in your config.log) you don't have some OpenGL libraries.

The solution is on the next post ( here ) and tells you to add these flags

 --disable-opencl \
 --disable-avs \
 --disable-cli \
 --disable-ffms \
 --disable-gpac \
 --disable-lavf \
 --disable-swscale


If you are trying to build the alsa lib and get "configure: error: C compiler cannot create executables"  you need to set the CC environment variable:

CC="arm-bcm2708hardfp-linux-gnueabi-gcc" ./configure --host=arm-linux --prefix=YOUR_PREFIX
If you get an error saying it can’t find arm-linux-gnueabihf/python2.7/pyconfig.h you can add
 --disable-python
to the configure flags (or get the relevant files from https://packages.debian.org/sid/armhf/libpython2.7-dev/filelist )








This is how I stream to my nginx-rtmp server.



raspivid -n -vs -ih -pf high -t 0 -ISO 800 -ex fixedfps -w 1280 -h 720 -fps $fps -g $((2*fps)) -b $bw -o - \
        | ../ffmpeg \
                -framerate $fps -r $fps -i - \
                -f alsa -ar $ar -ac 1 -itsoffset 10 -i btheadset \
                -c:v copy -r $fps -vsync drop \
                -c:a libfdk_aac -afterburner 0 -async 1 -ar 48000 -ac 1 -b:a 48k \
                -tune zerolatency -bufsize 100k \
                -rtmp_buffer 100 -rtmp_live live \
                -f flv rtmp://$HOST:1935/$STREAM/movie


The CPU load is ~20%.
If I only encode audio, the CPU load is about ~13%.

Friday, August 1, 2014

Place two (remote)videos side by side

I wanted to achieve something similar to this (at 30 seconds) while pulling from 2 video sources and 1 audio source

https://www.youtube.com/watch?v=0AJWmt5A62Y

Source:
http://stackoverflow.com/questions/9293265/ffmpeg-2-videos-transcoded-and-side-by-side-in-1-frame

video filter:
http://www.oodlestechnologies.com/blogs/PICTURE-IN-PICTURE-effect-using-FFMPEG

Important:

The overlay docs states:

  Be aware that frames are taken from each input video in timestamp
  order, hence, if their initial timestamps differ, it is a a good
  idea to pass the two inputs through a setpts=PTS-STARTPTS filter to
  have them begin in the same zero timestamp, as it does the example
  for the movie filter. 
 
 
http://ffmpeg.org/pipermail/ffmpeg-user/2013-June/015662.html

Half size:

ffmpeg -i http://192.168.1.123:8099/right.flv -i http://192.168.1.123:8099/left.flv
-filter_complex "[0:v]setpts=PTS-STARTPTS, scale=iw/2:ih/2, pad=2*iw:ih [left]; [1:v]setpts=PTS-STARTPTS, scale=iw/2:ih/2 [right]; [left][right] overlay=main_w/2:0" -i http://192.168.1.123:8099/audio.opus -c:v libx264 -c:a libfaac -tune zerolatency -preset ultrafast -crf 26 -maxrate 1000k -bufsize 1600k -f flv rtmp://107.170.84.207:1935/xorg/movie -y out$(date).mp4
True size:
ffmpeg -i http://192.168.1.14:8090/right.flv -i http://192.168.1.14:8090/left.flv
-filter_complex "[0:v]setpts=PTS-STARTPTS, scale=iw:ih, pad=2*iw:ih [left]; [1:v]setpts=PTS-STARTPTS, scale=iw:ih [right]; [left][right] overlay=main_w/2:0" -i http://192.168.1.14:8090/audio.mp3 -vcodec h264 -acodec libvo_aacenc -tune zerolatency -preset ultrafast -y out.mp4

Buffers kind-of-break the audio/video timing.
The videos start on a keyframe (ffserver's setting) so they usually start with up to a few seconds of delay.
It also takes a few seconds to start the 'join' process because it needs to wait for 2 keyframes

ATTENTION: The FPS will be determined by the first video setting, for example, -r 25 -vsync 1 will duplicate frames until 25 FPS is reached but each of the videos' framerate will be from the first video (which means that if the second video has more fps than the first one, it'll drop frames to match


Watermark:
http://stackoverflow.com/questions/10934420/ffmpeg-how-to-scale-a-video-then-apply-a-watermark
 
 
ffserver.conf

audio (https://ffmpeg.org/sample.html)

# MP3 audio
<Stream test.mp3>
Feed feed1.ffm
Format mp2
AudioCodec mp3
AudioBitRate 64
AudioChannels 1
AudioSampleRate 44100
NoVideo
</Stream>
 

rtmp push delay:
http://superuser.com/questions/769721/how-to-determine-stream-delay-in-ffmpeg