Advertisement
emkay443

FFMPEG Recording Script

Jul 10th, 2013
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.37 KB | None | 0 0
  1. #!/bin/bash
  2. # FFMPEG screen recording script
  3. # Capable of recording with microphone input (optional)
  4. # Compatible with Ubuntu 13.04 and the ffmpeg version in its repositories
  5. #
  6. # Author: Michael Koch (Emkay443) (m<DOT>koch<AT>emkay443<DOT>de)
  7. # Version: 2013-08-31
  8. # License: GNU General Public License v3 (http://www.gnu.de/documents/gpl-3.0.en.html)
  9.  
  10. ###################
  11. ## CONFIGURATION ##
  12. ###################
  13.  
  14. # Number of threads that should be used (0: automatic)
  15. threads=0
  16.  
  17. # Size and offset of your recording
  18. area_size=1920x1080
  19. offset_x=0
  20. offset_y=0
  21.  
  22. # Microphone device name (pulse for PulseAudio, hw:0,0 or similar for ALSA)
  23. mic_device=pulse
  24.  
  25. # Sound codec (should work with Ubuntu's ffmpeg automagically)
  26. sound_codec=pcm_s16le
  27.  
  28. # Video codec (should work out of the box as well)
  29. video_codec=libx264
  30.  
  31. # Franes per second, lower is faster (but more looks better)
  32. fps=30
  33.  
  34. # File directory and name
  35. file_dir=$HOME/Videos
  36. file_date=$(date +%d%m%Y_%H.%M.%S)
  37. video_file_extension="mov"
  38. sound_file_extension="wav"
  39. video_file_path="$file_dir/videocast_$file_date.$video_file_extension"
  40. sound_file_path="$file_dir/soundcast_$file_date.$sound_file_extension"
  41.  
  42. # Sound recording command
  43. # Example for PulseAudio: http://pastebin.com/MwMK3KUy
  44. # This has to exist and function properly, otherwise the sound record feature won't work!
  45. # Also, if your sound recording program/script uses something externally (like sox)
  46. # which doesn't get killed properly by this script, change sound_record_util to that external program
  47. # otherwise leave it the same as sound_record_cmd
  48. sound_record_cmd="$HOME/bin/pulserecord"
  49. sound_record_parameters="$sound_file_path"
  50. sound_record_util="$sound_record_cmd"
  51.  
  52. # You need a newer version of ffmpeg to combine video and audio without losing the microphone track!
  53. # Download the static version of ffmpeg from this site:
  54. # http://ffmpeg.gusari.org/static/
  55. # untar it (tar -xzvf) and move the binaries somewhere you can execute them from.
  56. #
  57. # If you downloaded a new, static version of ffmpeg, change the following variable to true
  58. # to enable combining the sound and video after recording
  59. # and change the path to your new, static ffmpeg binary file
  60. ffmpeg_static_enabled=false
  61. ffmpeg_static_path="$HOME/bin/ffmpeg_static"
  62.  
  63. #########################################################################################
  64. # WARNING: You should be careful when changing any code below. Modify at your own risk! #
  65. #########################################################################################
  66.  
  67. ######################
  68. ## HELPER VARIABLES ##
  69. ######################
  70.  
  71. video_record_nomic_cmd="ffmpeg -f x11grab -r $fps -s $area_size -i :0.0+$offset_x,$offset_y -vcodec $video_codec -vpre lossless_ultrafast -threads $threads $video_file_path"
  72. video_record_mic_cmd="ffmpeg -f alsa -i $mic_device -f x11grab -r $fps -s $area_size -i :0.0+$offset_x,$offset_y -acodec $sound_codec -vcodec $video_codec -vpre lossless_ultrafast -threads $threads $video_file_path"
  73.  
  74. ##################
  75. ## MAIN PROGRAM ##
  76. ##################
  77.  
  78. # If running in a terminal window, don't use graphical dialogs
  79. # else use zenity
  80. if [ -t 0 ]; then
  81.     echo "Stop recording with CTRL-C"
  82.     echo "Record with microphone? [Y|n]"
  83.     read ans
  84.     case $ans in
  85.         n|N)
  86.             record_with_mic=false
  87.             ;;
  88.         *)
  89.             record_with_mic=true
  90.             ;;
  91.     esac
  92.     echo "Record with sound? [Y|n]"
  93.     read ans
  94.     case $ans in
  95.         n|N)
  96.             record_with_sound=false
  97.             ;;
  98.         *)
  99.             record_with_sound=true
  100.             ;;
  101.     esac
  102.     if $record_with_sound; then
  103.         $sound_record_cmd $sound_record_parameters &
  104.     fi
  105.     if $record_with_mic; then
  106.         $video_record_mic_cmd
  107.     else
  108.         $video_record_nomic_cmd
  109.     fi 
  110.     if $record_with_sound; then
  111.         sound_record_pid="$(pidof -s -x $(basename $sound_record_util))"
  112.         if [ ! -z $sound_record_pid ]; then kill $sound_record_pid; fi
  113.     fi
  114.     if $ffmpeg_static_enabled; then
  115.         echo "Combine sound and video file? [Y|n]"
  116.         echo "This can take some time and CPU power!"
  117.         read ans
  118.         case $ans in
  119.             n|N)
  120.                 ffmpeg_static_enabled=false # We have to do this for the text output, without adding another variable
  121.                 ;;
  122.             *)
  123.                 tmp_video_file_path="/tmp/videocast_$file_date.$video_file_extension"
  124.                 $ffmpeg_static_path -i $video_file_path -i $sound_file_path -filter_complex amix=inputs=2:duration=first $tmp_video_file_path
  125.                 video_file_path="$file_dir/videocast_combined_$file_date.$video_file_extension"
  126.                 mv "$tmp_video_file_path" $video_file_path
  127.                 ;;
  128.         esac
  129.     fi
  130.     echo "Done."
  131.     echo "You can find your video at $video_file_path"
  132.     if $record_with_sound && ! $ffmpeg_static_enabled; then
  133.         echo "and your audio recording at $sound_file_path"
  134.         echo "To combine them properly, use a video editor like OpenShot or Pitivi"
  135.     fi
  136. else
  137.     if zenity --question --text "Record with microphone?" --title "Question"; then
  138.         record_with_mic=true
  139.     else
  140.         record_with_mic=false
  141.     fi
  142.     if zenity --question --text "Record with sound?" --title "Question"; then
  143.         record_with_sound=true
  144.     else
  145.         record_with_sound=false
  146.     fi
  147.     zenity --info --text "Stop recording with CTRL-C"
  148.  
  149.     if $record_with_mic; then
  150.         if $record_with_sound; then
  151.             xterm -e "($sound_record_cmd $sound_record_parameters &); $video_record_mic_cmd"
  152.         else
  153.             xterm -e "$video_record_mic_cmd"
  154.         fi     
  155.     else
  156.         if $record_with_sound; then
  157.             xterm -e "($sound_record_cmd $sound_record_parameters &); $video_record_nomic_cmd"
  158.         else
  159.             xterm -e "$video_record_nomic_cmd"
  160.         fi     
  161.     fi
  162.     if $record_with_sound; then
  163.         if $ffmpeg_static_enabled && zenity --question --text "Combine sound and video file?\n\nThis can take some time and CPU power!" --title "Question"; then
  164.             tmp_video_file_path="/tmp/videocast_$file_date.$video_file_extension"
  165.             xterm -e "$ffmpeg_static_path -i $video_file_path -i $sound_file_path -filter_complex amix=inputs=2:duration=first $tmp_video_file_path"
  166.             video_file_path="$file_dir/videocast_combined_$file_date.$video_file_extension"
  167.             mv "$tmp_video_file_path" "$video_file_path"
  168.             zenity --info --text "Done.\nYou can find your video at $video_file_path" --title "Success"
  169.         else
  170.             zenity --info --text "Done.\nYou can find your video at $video_file_path\nand your audio recording at $sound_file_path\n\nTo combine them properly, use a video editor like OpenShot or Pitivi" --title "Success"
  171.         fi
  172.     else
  173.         zenity --info --text "Done.\nYou can find your video at $video_file_path" --title "Success"
  174.     fi
  175. fi
  176.  
  177. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement