/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-07-10 20:03:08 UTC
  • Revision ID: git-v1:d55384577b22b2372b807d0db71abc00396b803a
* enc2firefox.sh
  - Simpelfied if-else-statement.

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