/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1

« back to all changes in this revision

Viewing changes to scripts/wav2mp4.sh

  • Committer: Gustav Hartvigsson
  • Date: 2024-11-02 21:10:35 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20241102211035-ev3nfuwqytbdabxd
* Fixed useful.inc.sh
* Fixed if statements not working in do-offline-updates.sh

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env bash
2
2
 
3
 
ARGS=()
4
 
ARGS="${@}"
5
 
 
6
 
__TMP_IMAGE_NAME=__SPEAKER_IMAGE.PNG
7
 
__OUT_NAME=
8
 
__IN_NAME=
 
3
######
 
4
# FILE NAME: wav2mp4.sh
 
5
#
 
6
# Authors:
 
7
#    Gustav Hartvigsson 2024
 
8
#    Distributed under the Cool Licence 1.1
 
9
#
 
10
# Changes
 
11
#
 
12
# 2021-01-31:
 
13
#   * Fixed up if statments
 
14
#   * Removed eval.
 
15
#
 
16
# 2022-03-22:
 
17
#   * Make script work with gifs, and Make gifs loop.
 
18
#
 
19
# 2024-07-10:
 
20
#   * Fixed some formating errors.
 
21
#   * Added sanity check.
 
22
#   * Use /tmp/ instead of writing to FS.
 
23
######
 
24
 
 
25
__SANITY=true
 
26
 
 
27
# FIXME: Use mktemp or mkdtemp instea of usin /tmp/ directly
 
28
__TMP_IMAGE_NAME=/tmp/__SPEAKER_IMAGE.PNG
 
29
__OUT_NAME=""
 
30
__IN_NAME=""
9
31
 
10
32
__USE_DIFFERENT_IMAGE=false
11
 
__OTHER_IMAGE_NAME=
12
 
 
13
 
__help () {
 
33
__OTHER_IMAGE_NAME=""
 
34
 
 
35
__LOOP=false
 
36
 
 
37
function __usage () {
14
38
  echo "wav2mp4.sh --- Convert audio to a video for upload. "
15
39
  echo " "
16
40
  echo "-i <audio.wav>        The input audio."
17
41
  echo " "
18
42
  echo "-o <video.mp4>        The output for the video."
19
43
  echo " "
20
 
  echo "--image <image.png>   Image to use. If non is provided a standard image"
 
44
  echo "--image <image.[png,gif]>"
 
45
  echo "                      Image to use. If none is provided a standard image"
21
46
  echo "                      will be used."
22
 
  echo " "
23
47
  echo ""
24
 
  exit
25
 
}
26
 
 
27
 
__do_cleanup () {
 
48
  exit 0
 
49
}
 
50
 
 
51
function __silent () {
 
52
  $@ >> /dev/null 2>&1
 
53
  return $?
 
54
}
 
55
 
 
56
function __sanity_check () {
 
57
  # Check that we have the tools needed.
 
58
  ___silent which ffmpeg 
 
59
 
 
60
  if [[ $? -gt 0 ]]; then
 
61
    echo "    Can't find tool \"ffmpeg\" (Required)."
 
62
    __SANITY=false
 
63
  fi
 
64
  
 
65
  if [[ $___SANITY == false ]]; then
 
66
    echo "Please install the missing tools."
 
67
    echo ""
 
68
    exit 1
 
69
  fi
 
70
}
 
71
 
 
72
function __do_cleanup () {
28
73
  rm $__TMP_IMAGE_NAME
29
74
}
30
75
 
31
 
___do_convertion () {
32
 
  ffmpeg -loop 1\
 
76
function __do_convertion_gif () {
 
77
  ffmpeg\
 
78
         -i "$__IN_NAME"\
 
79
         -ignore_loop 0\
33
80
         -i "$__TMP_IMAGE_NAME"\
34
 
         -i "$__IN_NAME"\
35
81
         -c:v libx264 -tune stillimage\
36
82
         -c:a aac -b:a 256k -pix_fmt yuv420p\
37
83
         -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -shortest\
38
84
         "$__OUT_NAME"
39
85
}
40
86
 
41
 
 
42
 
__parse_args() {
43
 
  if [ -z "$1" ]
 
87
function __do_convertion () {
 
88
  
 
89
  case $__TMP_IMAGE_NAME in
 
90
    ## FIXME: See if there are othre fileformats that need looping and such.
 
91
    *.gif)
 
92
      __do_convertion_gif
 
93
    ;;
 
94
    *)
 
95
    
 
96
    ffmpeg -loop 1\
 
97
           -i "$__TMP_IMAGE_NAME"\
 
98
           -i "$__IN_NAME"\
 
99
           -c:v libx264 -tune stillimage\
 
100
           -c:a aac -b:a 256k -pix_fmt yuv420p\
 
101
           -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -shortest\
 
102
           "$__OUT_NAME"
 
103
    ;;
 
104
  esac
 
105
}
 
106
 
 
107
 
 
108
function __parse_args () {
 
109
  if [[ -z "$1" ]]
44
110
  then
45
111
    echo "Try --help or -h."
46
112
    exit 1
49
115
  
50
116
  while [[ $# -gt 0 ]]
51
117
  do
52
 
    eval key="${1}"
53
118
    
54
119
    case "${1}" in
55
120
      -i)
56
 
      __IN_NAME="$2"
 
121
        __IN_NAME="$2"
57
122
      shift
58
123
      shift
59
124
      ;;
60
125
      -o)
61
 
      __OUT_NAME="$2"
 
126
        __OUT_NAME="$2"
62
127
      shift
63
128
      shift
64
129
      ;;
65
130
      --image)
66
 
      __USE_DIFFERENT_IMAGE=true
67
 
      __OTHER_IMAGE_NAME="$2"
68
 
      shift
 
131
        __USE_DIFFERENT_IMAGE=true
 
132
        __OTHER_IMAGE_NAME="$2"
 
133
      shift
 
134
      shift
 
135
      ;;
 
136
      --loop)
 
137
        __LOOP=true
69
138
      shift
70
139
      ;;
71
140
      -h|--help)
72
 
      __help
 
141
        __usage
73
142
      exit
74
143
      shift
75
144
      ;;
76
145
      *)
77
 
      __help
 
146
        __usage
78
147
      exit 1
79
148
      shift
80
149
      ;;
87
156
}
88
157
 
89
158
 
90
 
__write_image () {
 
159
function __write_image () {
91
160
  __IMAGE_BASE64="iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAQAAABh3xcBAAAZEHpUWHRSYXcgcHJvZmlsZSB0eXBl
92
161
IGV4aWYAAHja1ZtZdhy3GYXfsYosAfOwHIznZAdZfr6LalEURdmmnDyYdNRks7oK+Ic7AIjZ//n3
93
162
Mf/iKxebTUyl5paz5Su22Hznh2qfr+fV2Xj/vV8nvn5yP75v3v7geQ28hucPeT+vrvN++v6BEl/v
386
455
}
387
456
 
388
457
 
389
 
__main () {
390
 
  
391
 
  if [ $__USE_DIFFERENT_IMAGE = true ]
 
458
function __main () {
 
459
  __sanity_check
 
460
  __parse_args "${@}"
 
461
 
 
462
  if [[ $__USE_DIFFERENT_IMAGE == true ]]
392
463
  then
393
464
    # Let's not make this any more complicated.
394
465
    __TMP_IMAGE_NAME=$__OTHER_IMAGE_NAME
396
467
    __write_image
397
468
  fi
398
469
  
399
 
  if [ ! -e "$__IN_NAME" ]
 
470
  if [[ ! -e "$__IN_NAME" ]]
400
471
  then
401
472
    echo "missing input audio. Please provide."
402
473
    exit 1
403
474
  fi
404
475
  
405
 
  if [ $__OUT_NAME == "" ]
 
476
  if [[ $__OUT_NAME == "" ]]
406
477
  then
407
478
    echo "missing output file name. Please provide."
408
479
    exit 1
409
480
  fi
410
481
  
411
 
  
412
 
  ___do_convertion
413
 
  
414
 
  if [ $__USE_DIFFERENT_IMAGE = false ]
 
482
  __do_convertion
 
483
  
 
484
  if [[ $__USE_DIFFERENT_IMAGE == false ]]
415
485
  then
416
486
    __do_cleanup
417
487
  fi
418
488
}
419
489
 
420
 
__parse_args "${@}"
421
 
__main
 
490
__main "${@}"