2018 01 22 ffmpeg - hydrogeologger/pyduino GitHub Wiki


layout: post title: ffmpeg skills

1. record screen for education purposes 2018-01-22 15:26

{% highlight bash %} ffmpeg -f x11grab -r 1.5 -s 1280x1600 -i :1 -vb 20M out1.avi {% endhighlight %}

a better version of this would be {% highlight bash %} ffmpeg -f pulse -ac 2 -i default -f x11grab -r 10 -s 1366x768 -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -threads 0 -y output.mkv {% endhighlight %}

below works very well for a webcam video and audio:

ffmpeg  -thread_queue_size 5120 -y -f alsa -i plughw:CARD=U0x46d0x991,DEV=0 -f v4l2 -i /dev/video0 -acodec aac -strict -2 -ac 1 -b:a 64k -vcodec libx264 -b:v 300k -r 30 -g 30 prueba1.mp4

-acodec libmp3lame also works!!!

where plughw:CARD=U0x46d0x991,DEV=0 is from command

arecord -L

below works well with half of the mac screen input, voice record from webcam:

ffmpeg  -thread_queue_size 5120 -y -f alsa -i plughw:CARD=U0x46d0x991,DEV=0 -f  x11grab -r 10 -s 1280x1600 -i :0.0  -acodec libmp3lame -strict -2 -ac 1 -b:a 64k -vcodec libx264 -b:v 3000k -r 30 -g 30 prueba1_.mp4

where

-b:v 3000k bit rate for video output, 3000k means 32MB per minutes, with clear youtube output,for half of my apple screen -s 1600x1200 screen resolution

reference: https://askubuntu.com/questions/167360/how-to-format-the-ffmpeg-command-to-record-sound-from-my-webcam

  1. extract one frame from a video
ffmpeg -i video.webm -ss 00:00:07.000 -vframes 1 thumb.jpg 
  1. output video
ffmpeg -r 3 -i %05d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p output.mp4
  1. shell command to get pixel size of an image
identify color.jpg
> color.jpg JPEG 1980x650 1980x650+0+0 8-bit DirectClass 231KB 0.000u 0:00.000
  1. crop sigle image
convert -crop 860x720+980+100 screenshot.png test.png

crop a group of images

We’ll make a new folder called ‘cropped’ to keep the cropped images in. Then we’ll run the command with a couple of changes:

mogrify -crop 860x720+980+100 -path ./cropped *.png

2. stitch photo into video

# sort all the photo by time and name as 000xx.png 
 ls | cat -n | while read n f; do mv "$f" "$(printf "%05d" $n).png"; done
# the framerate means the number of photos displayed per seconds
# ffmpeg -framerate 3 -i %05d.png -c:v libx264 -vf "fps=25,format=yuv420p" output.mp4   CZ20217 this code is found to produce a video that can not be inserted to PowerPoint
ffmpeg -framerate 20 -i %05d.png -c:v mpeg1video -vf "fps=25" output_column2_back_a3.mp4

It is necessary to adjust the format of the piture accordingly, and the framerate to improve the visualisation.

3. concatenate video using ffmpeg (TO190808)

At one stage, I will need to record video for undergraduate and postgraduate students. shareX is found to be one of the best to record this. after recording, several file needs to be merged. which can be done by the script below:
echo file 1.mp4 > a.txt
echo file 2.mp4 >> a.txt
echo file 3.mp4 >> a.txt
echo file 4.mp4 >> a.txt
ffmpeg -f concat -i a.txt -c copy output.mp4

reference: https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg