summaryrefslogtreecommitdiff
path: root/localvideodump
blob: ba4880caf4053254f4aa5cb5069adf55af67ad1a (plain)
  1. #!/bin/sh
  2. # Copyright © 2012, 2013 Jonas Smedegaard <dr@jones.dk>
  3. # Description: record most possible video data using least CPU
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3, or (at your option)
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # Depends: libav-tools, alsa-utils, v4l-utils
  19. #
  20. # TODO: add --help option
  21. # TODO: make inputs overrideable
  22. # TODO: optionally limit number of inputs
  23. # TODO: make codecs, rates etc. configurable
  24. # TODO: auto-limit number of video-inputs per USB channel
  25. # TODO: include SMTPE channel
  26. # TODO: Check and ask if outfile already exist
  27. # TODO: Add --force option to skip asking for overwriting
  28. set -e
  29. #melt cam_Microsoft_LifeCam_Studio.mlt -consumer avformat:x.avi vcodec=ffv1 an=1
  30. output="${1:-dump.mkv}"
  31. # resolve audio inputs: default PCM of all ALSA cards with input devices
  32. ainputs="$(arecord -l | perl -anE '/^card/ and say $F[2]')"
  33. #ainputs="CARD=Generic_1,DEV=0"
  34. # FIXME: detect and avoid more than one device on same USB channel:
  35. # [video4linux2 @ 0xed7e00] ioctl(VIDIOC_STREAMON): No space left on device
  36. # /dev/video2: No space left on device
  37. # maybe with $(v4l2-ctl --list-devices)
  38. vinputs="$(ls -1 /dev/video*)"
  39. #vinputs="$(ls -1 /dev/video* | grep -v video1)"
  40. #vinputs="/dev/video0 /dev/video1 /dev/video3"
  41. #vinputs="/dev/video0"
  42. # TODO: probe and optimize V4L settings (highest 16/9 size at >= 20fps
  43. inrate=20
  44. #inrate=10
  45. outrate=$inrate # seems this ensures fixed rate when input is flaky
  46. #vcodec=huffyuv # more common
  47. vcodec=ffvhuff # more compact, supports YV12 colorspace
  48. #vcodec=ffv1 # most compact, supports RGBA colorspace, too CPU-hungry
  49. acodec=pcm_s16le
  50. vf="${vf:+$vf }-vf setpts=(RTCTIME-RTCSTART)/(TB*1000000)" # current time
  51. vf="${vf:+$vf }-vf hqdn3d" # high-quality denoise (good for webcams)
  52. font='/usr/share/fonts/truetype/droid/DroidSans.ttf'
  53. #vf="${vf:+$vf }-vf "drawtext=fontfile=$font: timecode='09\:57\:00\:00': r=25: x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000000@1"}"
  54. #vf="${vf:+$vf }-vf "drawtext=fontfile=DroidSans.ttf:'timecode=09\:57\:00\:00':r=25:x=\(w-tw\)/2:y=h-\(2*lh\):fontcolor=white:box=1:boxcolor=0x00000000@1"}"
  55. parse_v4l2_formats() {
  56. v4l2-ctl --list-formats-ext -d "$1" | perl -n \
  57. -e '/^\tIndex\h*:/ and $type=$format=$raw=$size=$speed=undef;'\
  58. -e '/^\tType\h*: (Video Capture)/ and $type=$1;'\
  59. -e '/^\tPixel Format: '\''(\w+)'\''( \(compressed\))?/ and $format=$1 and $raw=int(!($2));'\
  60. -e '/^\t\tSize: Discrete (\d+x\d+)/ and $size=$1;'\
  61. -e 'next unless ($type and $format and $size);'\
  62. -e '/^\t\t\tInterval: .* \((\d+)\.\d+ fps\)/ and $speed=$1;'\
  63. -e 'next unless ($speed >= '$inrate');'\
  64. -E 'say "$raw $size $format"' \
  65. | sort -rV
  66. }
  67. # expand arguments
  68. i=0
  69. ainargs=
  70. amaps=
  71. for input in $ainputs; do
  72. # TODO: handle varying descriptions (not just pick first entry)
  73. # TODO: handle more descriptions (not just assume stereo)
  74. achannels=
  75. achannelsdesc="$(amixer -c $input | grep '^ Capture channels:' | head -n 1)"
  76. case $achannelsdesc in
  77. *': Mono')
  78. achannels=1
  79. ;;
  80. esac
  81. ainargs="${ainargs:+$ainargs }-f alsa -ar 44100 ${achannels:+-ac $achannels} -i hw:$input"
  82. amaps="${amaps:+$amaps }-map $i:a"
  83. i=$(expr $i + 1)
  84. done
  85. vinargs=
  86. vmaps=
  87. for input in $vinputs; do
  88. read raw size format dummy <<FORMATS
  89. $(parse_v4l2_formats $input)
  90. FORMATS
  91. case $format in
  92. YUYV)
  93. format=yuyv422
  94. ;;
  95. MJPG)
  96. format=mjpeg
  97. ;;
  98. esac
  99. [ -n "$format" ] || { echo "Skipping unsuitable input $input"; continue; }
  100. vinargs="${vinargs:+$vinargs }-f video4linux2 -input_format $format ${inrate:+-framerate $inrate }-s $size -i $input"
  101. vmaps="${vmaps:+$vmaps }-map $i:v:0"
  102. i=$(expr $i + 1)
  103. done
  104. voutargs=${vinputs:+-c:v $vcodec $vf ${outrate:+-r $outrate}}
  105. aoutargs=${ainputs:+-c:a $acodec}
  106. echo avconv -threads auto $ainargs $vinargs $vmaps $amaps $voutargs $aoutargs -y "$output"
  107. exec avconv -threads auto $ainargs $vinargs $vmaps $amaps $voutargs $aoutargs -y "$output"