pict-converter.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/bash
  2. # Bachir Soussi Chiadmi 06/2016
  3. # https://trac.ffmpeg.org/wiki/Create%20a%20video%20slideshow%20from%20images
  4. # needs ffmep and exiftool
  5. function convert {
  6. now=$(date +"%Y-%m-%d--%T")
  7. echo "Timelaps conversion | $now"
  8. pf=12
  9. vf=24
  10. mkdir tmp_videos
  11. mkdir tmp_metadata
  12. mkdir tmp_pictures
  13. rm -f tmp_pictures/*
  14. rm -f tmp_videos/*
  15. rm -f tmp_metadata/*
  16. x=1
  17. for i in public_html/pictures/*.jpg; do
  18. counter=$(printf %06d $x)
  19. ln "$i" tmp_pictures/img"$counter".jpg
  20. x=$(($x+1))
  21. done
  22. echo "pictframerate:$pf, videoframerate=$vf"
  23. echo 'Video conversion'
  24. echo "creating raw timelaps"
  25. ffmpeg -y \
  26. -framerate $pf \
  27. -i tmp_pictures/img%06d.jpg \
  28. -r $vf -pix_fmt yuv420p -vcodec libx264 \
  29. tmp_videos/timelaps-raw.mp4
  30. echo "optimizing responsive videos"
  31. ffmpeg -y -i tmp_videos/timelaps-raw.mp4 -vf "scale=1920:-1, crop=in_w:1080" tmp_videos/timelaps-1080p.mp4
  32. ffmpeg -y -i tmp_videos/timelaps-raw.mp4 -vf "scale=1280:-1, crop=in_w:720" tmp_videos/timelaps-720p.mp4
  33. ffmpeg -y -i tmp_videos/timelaps-raw.mp4 -vf "scale=720:-1, crop=in_w:480" tmp_videos/timelaps-480p.mp4
  34. ffmpeg -y -i tmp_videos/timelaps-raw.mp4 -vf "scale=640:-1, crop=in_w:340" tmp_videos/timelaps-360p.mp4
  35. ffmpeg -y -i tmp_videos/timelaps-raw.mp4 -vf "scale=360:-1, crop=in_w:240" tmp_videos/timelaps-240p.mp4
  36. echo "updating production videos"
  37. mv -f tmp_videos/* public_html/videos/
  38. echo "metadata export"
  39. l=0
  40. for pict in public_html/pictures/*.jpg; do
  41. l=$((l+1))
  42. done
  43. f=0
  44. echo -e '{' > tmp_metadata/frames.json
  45. echo -e '\t"frames":[' >> tmp_metadata/frames.json
  46. for pict in public_html/pictures/*.jpg; do
  47. # SRC
  48. src="${pict/public_html\/pictures\//}"
  49. # TIME
  50. datetime="${pict/public_html\/pictures\/hehe-/}"
  51. datetime="${datetime/.jpg/}"
  52. Y="${datetime:0:4}"
  53. m="${datetime:4:2}"
  54. d="${datetime:6:2}"
  55. h="${datetime:9:2}"
  56. min="${datetime:11:2}"
  57. sec="${datetime:13:2}"
  58. datetime="$Y-$m-$d $h:$min:$sec"
  59. datetime="${datetime/ /T}"
  60. # echo "datetime : $datetime"
  61. # datetime= $($datetime | awk /([0-9]{4})([0-9]{2})([0-9]{2})-([0-9]{2})([0-9]{2})([0-9]{2})/ {print $1-$2-$3T$4:$5:$6})
  62. # datetime=`exiftool 2>/dev/null -S --DateTimeOriginal -p '$DateTimeOriginal' -d %Y-%m-%dT%T "$pict"`
  63. # GPS
  64. gps=`exiftool 2>/dev/null -n -p '$GPSLatitude,$GPSLongitude' $pict`
  65. # FRAME
  66. currenttime=$(((1/$pf)*$f))
  67. frame="\t\t{\"src\":\"$src\", \"datetime\":\"$datetime\", \"gps\":\"$gps\", \"currenttime\":\"$currenttime\", \"id\":\"$f\"}"
  68. if [[ $f != $(($l-1)) ]]; then
  69. frame="$frame,"
  70. fi
  71. echo -e "$frame" >> tmp_metadata/frames.json
  72. f=$(($f+1))
  73. done
  74. echo -e '\t]' >> tmp_metadata/frames.json
  75. echo -e '}' >> tmp_metadata/frames.json
  76. mv -f tmp_metadata/* public_html/metadata/
  77. }
  78. while true; do
  79. day=$(date +"%Y-%m-%d")
  80. logfile="logs/convert-$day.log"
  81. touch $logfile
  82. convert | tee -a $logfile
  83. sleep 10m
  84. done