summaryrefslogtreecommitdiff
path: root/localvideodump
blob: 2e6875feb8c6004189a18cd6363ee26502d2785a (plain)
  1. #!/bin/sh
  2. # Copyright © 2012-2014 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: ffmpeg, 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. output="${1:-dump.nut}"
  30. # resolve audio inputs: default PCM of all ALSA cards with input devices
  31. ainputs="$(arecord -l | grep -Po '^card \d+: \K\S+')"
  32. #ainputs="CARD=Generic_1,DEV=0"
  33. # FIXME: detect and avoid more than one device on same USB channel:
  34. # [video4linux2 @ 0xed7e00] ioctl(VIDIOC_STREAMON): No space left on device
  35. # /dev/video2: No space left on device
  36. # maybe with $(v4l2-ctl --list-devices)
  37. vinputs="$(ls -1 /dev/video*)"
  38. #vinputs="$(ls -1 /dev/video* | grep -v video1)"
  39. #vinputs="/dev/video0 /dev/video1 /dev/video3"
  40. #vinputs="/dev/video0"
  41. # TODO: probe and optimize V4L settings (highest 16/9 size at >= 20fps
  42. inrate=20
  43. #inrate=10
  44. outrate=$inrate # seems this ensures fixed rate when input is flaky
  45. vcodec=copy # uncompressed
  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=copy
  50. #acodec=pcm_s16le
  51. parse_v4l2_formats() {
  52. v4l2-ctl --list-formats-ext -d "$1" | perl -n \
  53. -e '/^\tIndex\h*:/ and $type=$format=$raw=$size=$speed=undef;'\
  54. -e '/^\tType\h*: (Video Capture)/ and $type=$1;'\
  55. -e '/^\tPixel Format: '\''(\w+)'\''( \(compressed\))?/ and $format=$1 and $raw=int(!($2));'\
  56. -e '/^\t\tSize: Discrete (\d+x\d+)/ and $size=$1;'\
  57. -e 'next unless ($type and $format and $size);'\
  58. -e '/^\t\t\tInterval: .* \((\d+)\.\d+ fps\)/ and $speed=$1;'\
  59. -e 'next unless ($speed >= '$inrate');'\
  60. -E 'say "$raw $size $format"' \
  61. | sort -rV
  62. }
  63. # expand arguments
  64. i=0
  65. ainargs=
  66. amaps=
  67. for input in $ainputs; do
  68. # TODO: handle varying descriptions (not just pick first entry)
  69. # TODO: handle more descriptions (not just assume stereo)
  70. achannels=
  71. achannelsdesc="$(amixer -c $input | grep '^ Capture channels:' | head -n 1)"
  72. case $achannelsdesc in
  73. *': Mono')
  74. achannels=1
  75. ;;
  76. esac
  77. ainargs="${ainargs:+$ainargs }-f alsa -ar 44100 ${achannels:+-ac $achannels} -i hw:$input"
  78. amaps="${amaps:+$amaps }-map $i:a"
  79. i=$(expr $i + 1)
  80. done
  81. vinargs=
  82. vmaps=
  83. for input in $vinputs; do
  84. read raw size format dummy <<FORMATS
  85. $(parse_v4l2_formats $input)
  86. FORMATS
  87. case $format in
  88. YUYV)
  89. format=yuyv422
  90. ;;
  91. MJPG)
  92. format=mjpeg
  93. ;;
  94. esac
  95. [ -n "$format" ] || { echo "Skipping unsuitable input $input"; continue; }
  96. vinargs="${vinargs:+$vinargs }-f video4linux2 -input_format $format ${inrate:+-framerate $inrate }-s $size -threads auto -i $input"
  97. vmaps="${vmaps:+$vmaps }-map $i:v:0"
  98. i=$(expr $i + 1)
  99. done
  100. voutargs=${vinputs:+-c:v $vcodec ${outrate:+-r $outrate}}
  101. aoutargs=${ainputs:+-c:a $acodec}
  102. pasuspender=
  103. test ! -x /usr/bin/pasuspender | pasuspender=pasuspender
  104. which ffmpeg > /dev/null && ffmpeg=ffmpeg || ffmpeg=avconv
  105. echo $pasuspender $ffmpeg $ainargs $vinargs $vmaps $amaps $voutargs $aoutargs -y "$output"
  106. exec $pasuspender $ffmpeg $ainargs $vinargs $vmaps $amaps $voutargs $aoutargs -y "$output"