Advertisement
Guest User

Untitled

a guest
Feb 28th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Global variables
  4. DATA_DIR="$HOME/.lapser"
  5.  
  6. PROFILE_DIR=''
  7. LABEL_FILE=''
  8. CONFIG_FILE=''
  9. SHOTS_CURRENT_DIR=''
  10. SHOTS_NOT_CONVERTED_DIR=''
  11. SHOTS_CONVERTED_DIR=''
  12. MOVIES_NOT_UPLOADED_DIR=''
  13. MOVIES_UPLOADED_DIR=''
  14.  
  15. set_profile_dirs(){
  16. profile=$1
  17. PROFILE_DIR="$DATA_DIR"/profiles/"$profile"
  18. LABEL_FILE="$PROFILE_DIR"/working_on.txt
  19. CONFIG_FILE="$PROFILE_DIR"/config.cfg
  20. LOG_FILE="$PROFILE_DIR"/log.txt
  21. SHOTS_CURRENT_DIR="$PROFILE_DIR"/current_shots
  22. SHOTS_NOT_CONVERTED_DIR="$PROFILE_DIR"/shots_archived_not_converted
  23. SHOTS_CONVERTED_DIR="$PROFILE_DIR"/shots_archived_converted
  24. MOVIES_NOT_UPLOADED_DIR="$PROFILE_DIR"/movies_not_uploaded
  25. MOVIES_UPLOADED_DIR="$PROFILE_DIR"/movies_uploaded
  26.  
  27. for d in "$DATA_DIR" "$DATA_DIR"/profiles "$DATA_DIR"/profiles/default "$PROFILE_DIR" "$SHOTS_CURRENT_DIR" "$SHOTS_NOT_CONVERTED_DIR" "$SHOTS_CONVERTED_DIR" "$MOVIES_NOT_UPLOADED_DIR" "$MOVIES_UPLOADED_DIR" ;do
  28. if [ ! -d "$d" ]; then
  29. mkdir "$d"
  30. fi
  31. done
  32.  
  33. for f in "$LABEL_FILE" "$LOG_FILE" "$CONFIG_FILE";do
  34. if [ ! -f "$f" ]; then
  35. echo -n > "$f"
  36. fi
  37. done
  38. }
  39.  
  40. set_basic_dirs(){
  41.  
  42. for d in "$DATA_DIR" "$DATA_DIR"/profiles "$DATA_DIR"/profiles/default;do
  43. if [ ! -d "$d" ]; then mkdir "$d"; fi
  44. done
  45.  
  46. }
  47.  
  48.  
  49. yad_message(){
  50. yad
  51. --width=600
  52. --title="Config"
  53. --text="$1"
  54. --button="OK:0"
  55. --center
  56. }
  57.  
  58. first_current_shot(){
  59. file=`ls -d "$SHOTS_CURRENT_DIR"/* | head -1`
  60. file=`basename $file`
  61. file="${file%.*}"
  62. echo "$file"
  63. }
  64.  
  65. last_current_shot(){
  66. file=`ls -d "$SHOTS_CURRENT_DIR"/* | tail -1`
  67. file=`basename $file`
  68. file="${file%.*}"
  69. echo "$file"
  70. }
  71.  
  72.  
  73. how_many_current_shots(){
  74. ret=`find "$SHOTS_CURRENT_DIR" -maxdepth 1 -type f | wc -l`
  75. echo "$ret"
  76. }
  77.  
  78.  
  79. monitor(){
  80. # DEBUG, PUT BACK TO 15
  81. #threshold='15';
  82. threshold='1';
  83. timestamp=$SECONDS
  84. while sleep 1; do
  85.  
  86. echo 100
  87.  
  88. new_timestamp=$SECONDS
  89. delta=`expr $new_timestamp - $timestamp`;
  90.  
  91. if [ $delta -ge $threshold ];then
  92.  
  93. # Prepare timestamp for next cycle
  94. timestamp=$new_timestamp
  95.  
  96. # Get label variables ready
  97. now=$(date +"%Y-%m-%d_%H.%M.%S")
  98. label=`cat "$LABEL_FILE"`
  99.  
  100. # Make screenshot
  101. import -resize 800 -window root /tmp/shot.${PID}.jpg
  102. convert /tmp/shot.${PID}.jpg -gravity Southwest -background black -fill white -splice 0x22 -pointsize 18 -annotate +0+0 "$now: $label" "$SHOTS_CURRENT_DIR"/"$now".jpg
  103.  
  104. fi
  105. done
  106. }
  107.  
  108.  
  109.  
  110. the_program(){
  111.  
  112. profile=$1
  113.  
  114. if [ -z "$profile" ];then
  115. profile='default';
  116. fi
  117.  
  118. set_profile_dirs $profile
  119. PID=$$
  120.  
  121. # Read the config file
  122. source "$CONFIG_FILE"
  123.  
  124. working_on=`cat $LABEL_FILE`
  125.  
  126. while true;do
  127.  
  128. ##############
  129. # MAIN MENU
  130. ##############
  131.  
  132. # Show main dialog, getting $ret and $res
  133. res=$(yad
  134. --width=600
  135. --title="Lapser automatic screenshots - $profile"
  136. --text="Press the button to start logging..."
  137. --form
  138. --field="Working on..."
  139. --button="Start capturing:2"
  140. --button="Archive:6"
  141. --button="Convert to movie:8"
  142. --button="Upload:10"
  143. --button="Config:4"
  144. --button="Cancel:17"
  145. --center
  146. "$working_on" )
  147.  
  148. ret=$?
  149.  
  150. # This gets saved regardless (unless ESCAPing or CANCELing)
  151. if [ $ret -ne 17 -a $ret -ne 252 ];then
  152. working_on=`echo $res | cut -d '|' -f 1`
  153. echo "$working_on" > $LABEL_FILE
  154. fi
  155.  
  156. # #1: Start capturing
  157. if [ $ret -eq 2 -o $ret -eq 0 ];then
  158. first_shot=$(first_current_shot)
  159. how_many=$(how_many_current_shots)
  160. if [ $how_many -eq 0 ];then
  161. extra_text="This is a first capture after archiving"
  162. else
  163. first_shot=$(first_current_shot)
  164. extra_text="This capture started on $first_shot"
  165. fi
  166.  
  167. d=$(date +"%Y-%m-%d_%H.%M.%S")
  168. label=`cat "$LABEL_FILE"`
  169. echo $d - START $label >> $LOG_FILE
  170. monitor | yad --progress --title="Capturing for $profile" --progress-text="Capturing in progress. $extra_text" --text="Press Cancel to stop capturing" --center
  171. d=$(date +"%Y-%m-%d_%H.%M.%S")
  172. echo $d - STOP $label >> $LOG_FILE
  173.  
  174. # #4: Config
  175. elif [ $ret -eq 4 ];then
  176.  
  177. res=$(yad
  178. --width=600
  179. --title="Config"
  180. --text="COnfiguration options"
  181. --form
  182. --field="User"
  183. --field="Password:H"
  184. --field="SSH Server"
  185. --button="Save:2"
  186. --button="Cancel:1"
  187. --center
  188. "$cfg_user" "$cfg_password" "$cfg_server" )
  189.  
  190. ret=$?
  191.  
  192. if [ $ret -eq 2 -o $ret -eq 0 ];then
  193. cfg_user=`echo $res | cut -d '|' -f 1`
  194. cfg_password=`echo $res | cut -d '|' -f 2`
  195. cfg_server=`echo $res | cut -d '|' -f 3`
  196. echo -e "cfg_user='${cfg_user}'ncfg_password='${cfg_password}'ncfg_server='${cfg_server}'n" > $CONFIG_FILE
  197. source "$CONFIG_FILE"
  198.  
  199. yad_message "Configuration saved!"
  200. fi
  201.  
  202. # #6: Archive
  203. elif [ $ret -eq 6 ];then
  204.  
  205. how_many=$(how_many_current_shots)
  206.  
  207. if [ $how_many -le 4 ];then
  208. yad_message "Less than 4 screenshots taken, too early to archive"
  209. else
  210.  
  211. first_file=$(first_current_shot)
  212. last_file=$(last_current_shot)
  213.  
  214. if [ -z "$last_file" -o -z "$first_file" ];then
  215. yad_message "Error working out the name of the destination folder!"
  216. else
  217.  
  218. log=$(cat $LOG_FILE)
  219.  
  220. yad
  221. --width=600
  222. --height=400
  223. --title="Are you sure?"
  224. --text="This will archive the current time lapse.nn(From $first_file to $last_file)nn"
  225. --form
  226. --field="Log entries that will be attached to the archive:TXT"
  227. --center
  228. "$log"
  229.  
  230. ret=$?
  231.  
  232. if [ $ret -eq 0 ];then
  233.  
  234. to="$first_file"_TO_"$last_file"
  235. mkdir "$SHOTS_NOT_CONVERTED_DIR"/"$to"
  236. mv "$SHOTS_CURRENT_DIR"/* "$SHOTS_NOT_CONVERTED_DIR"/"$to"
  237. cp "$LOG_FILE" "$SHOTS_NOT_CONVERTED_DIR"/"$to"
  238. > "$LOG_FILE"
  239. yad_message "Archive created, timestamp: $to"
  240. fi
  241. fi
  242. fi
  243.  
  244. # #8: Convert to movie
  245. elif [ $ret -eq 8 ];then
  246.  
  247. list='';
  248. for f in "$SHOTS_NOT_CONVERTED_DIR"/*;do
  249. f=`basename $f`
  250. exc='!'
  251. list="${list}${exc}${f}"
  252. done
  253.  
  254. pick=$(yad
  255. --width=600
  256. --title="Which archive do you want to convert?"
  257. --form
  258. --field="Pick an archive:CB"
  259. --center
  260. "$list")
  261. ret=$?
  262.  
  263. if [ $ret -ne 1 -a $ret -ne 252 ];then
  264. pick=`echo $pick | cut -d '|' -f 1`
  265.  
  266. ffmpeg28 -y -framerate 4 -pattern_type glob -i "$SHOTS_NOT_CONVERTED_DIR"/"$pick"/'*.jpg' -b:v 800k /tmp/video.${PID}.mp4
  267.  
  268. ret=$?
  269.  
  270. if [ "$ret" -ne 0 ];then
  271. yad_message "Video creation failed"
  272. else
  273. # Move the screenshots to the "CONVERTED" directory
  274. mv "$SHOTS_NOT_CONVERTED_DIR"/"$pick" "$SHOTS_CONVERTED_DIR"
  275. # Move the freshly generated video over
  276. mv /tmp/video.${PID}.mp4 "$MOVIES_NOT_UPLOADED_DIR"/"$pick".mp4
  277. # Copy the log file over, named as the video
  278. cp "$SHOTS_CONVERTED_DIR"/"$pick"/log.txt "$MOVIES_NOT_UPLOADED_DIR"/"$pick".log
  279. yad_message "Conversion successful!"
  280. fi
  281. fi
  282.  
  283.  
  284. # #8: Upload
  285. elif [ $ret -eq 10 ];then
  286.  
  287. yad
  288. --width=600
  289. --title="Are you sure?"
  290. --text="This will upload the existing movies to the remote servernAre you sure?"
  291. --center
  292. ret=$?
  293.  
  294. # #4: The end
  295. elif [ $ret -eq 4 -o $ret -eq 252 ];then
  296. return
  297. fi
  298.  
  299. done;
  300.  
  301.  
  302. # TODO:
  303. # ----
  304.  
  305. # * Fix cycle, so that first yad calls second one and back to first
  306. # * Write rsync call to upload/move videos
  307.  
  308. # * Check if this command makes better videos smaller
  309. # convert 0.png -background black -flatten +matte 0_opaque.png
  310. # http://stackoverflow.com/questions/3561715/using-ffmpeg-to-encode-a-high-quality-video|
  311. }
  312.  
  313.  
  314.  
  315.  
  316.  
  317. set_basic_dirs
  318.  
  319. while true;do
  320.  
  321. list='';
  322. for f in "$DATA_DIR"/profiles/*;do
  323. f=`basename $f`
  324. exc='!'
  325. list="${list}${exc}${f}"
  326. done
  327. list="${list}${exc}Make new profile"
  328.  
  329. pick=$(yad
  330. --width=600
  331. --title="Which profile do you want to enter?"
  332. --form
  333. --field="Pick a profile:CB"
  334. --center
  335. "$list")
  336. ret=$?
  337.  
  338. if [ $ret -ne 1 -a $ret -ne 252 ];then
  339.  
  340. pick=`echo $pick | cut -d '|' -f 1`
  341. if [ "$pick" == 'Make new profile' ];then
  342.  
  343. res=$(yad
  344. --width=600
  345. --title="Create new profile"
  346. --form
  347. --field="New profile"
  348. --button="Create:2"
  349. --button="Cancel:1"
  350. --center )
  351.  
  352. ret=$?
  353.  
  354. if [ $ret -eq 2 -o $ret -eq 0 ];then
  355. res=`echo $res | cut -d '|' -f 1`
  356. set_profile_dirs $res
  357. fi
  358.  
  359. else
  360. the_program $pick
  361. fi
  362. else
  363. exit 0
  364. fi
  365.  
  366. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement