/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1
11 by Gustav Hartvigsson
General fixup.
1
#!/usr/bin/env bash
2
####
3
# FILE NAME: enc2firefox.sh
4
#
5
# Encodes (mp4/x264) videos so they can be played in firefox.
6
#
7
# Updates:
8
# 2020-09-05:
9
#   * Removed an eval that made no sense.
10
#   * Added FJ mode for those spicy memes.
11
#
12
# 2021-01-13:
13
#   * Fixed up if statments
14
#
15
####
16
17
__IN_NAME=
18
__OUT_NAME=
19
__USE_WEBM=false
20
__USE_FJ=false
21
22
__help () {
23
  echo "enc2firefox.sh -- Make Videos Playable in Firefox"
24
  echo " "
25
  echo "-i <input video file>     Input Video File."
26
  echo " "
27
  echo "-o <output video file>    Output Video File"
28
  echo "                          (.mp4 will be added to the end)."
29
  echo " "
30
  echo "-m                        output webm instead"
31
  echo "                          (Will change ending to .webm)."
32
  echo " "
33
  echo "-f                        Use FJ settings. Low bandwith, small size."
34
  echo "                          (Will append .mp4 to the end)."
35
  echo " "
36
  exit
37
}
38
39
__enc_mp4 () {
40
  ffmpeg -i "$__IN_NAME"\
41
            -c:v libx264\
42
            -preset slower\
43
            -crf 22\
44
            -pix_fmt yuv420p\
45
            -c:a aac\
46
            -b:a 128k\
47
            "$__OUT_NAME".mp4
48
}
49
50
__enc_fj () {
51
  ffmpeg -i "$__IN_NAME"\
52
            -c:v libx264\
53
            -preset slower\
54
            -pix_fmt yuv420p\
55
            -b:v 1024k\
56
            -s hd720\
57
            -r 30\
58
            -c:a aac\
59
            -b:a 128k\
60
            "$__OUT_NAME".mp4
61
}
62
63
__enc_webm () {
64
  ffmpeg -i "$__IN_NAME"\
65
            -c:v libvpx-vp9\
66
            -b:v 0\
67
            -crf 20\
68
            -c:a libopus\
69
            "$__OUT_NAME".webm
70
}
71
72
__parse_args() {
73
  if [[ -z "$1" ]]
74
  then
75
    echo "Try --help or -h."
76
    exit 1
77
  fi
78
  
79
  
80
  while [[ $# -gt 0 ]]
81
  do
82
    
83
    case "${1}" in
84
      -i)
85
      __IN_NAME="$2"
86
      shift
87
      shift
88
      ;;
89
      -o)
90
      __OUT_NAME="$2"
91
      shift
92
      shift
93
      ;;
94
      -m)
95
      __USE_WEBM=true
96
      shift
97
      ;;
98
      -f)
99
      __USE_FJ=true
100
      shift
101
      ;;
102
      -h|--help)
103
      __help
104
      shift
105
      ;;
106
      *)
107
      __help
108
      exit 1
109
      shift
110
      ;;
111
      --)
112
      shift
113
      break
114
      ;;
115
    esac
116
  done
117
}
118
119
__main () {
120
  if [[ ! -e "$__IN_NAME" ]]
121
  then
122
    echo "missing input audio. Please provide."
123
    exit 1
124
  fi
125
  
126
  if [[ $__OUT_NAME == "" ]]
127
  then
128
    echo "missing output file name. Please provide."
129
    exit 1
130
  fi
131
  
132
  
133
  if [[ $__USE_WEBM == true ]]
134
  then
135
    if [[ $__IN_NAME == $__OUT_NAME.webm ]]
136
    then
137
      echo "Filenames can't be the same."
138
      exit
139
    fi
140
    __enc_webm
141
  elif [[ $__USE_FJ == true ]]
142
  then
143
    if [[ $__IN_NAME == $__OUT_NAME.mp4 ]]
144
    then
145
      echo "Filenames can't be the same."
146
      exit
147
    fi
148
    __enc_fj
149
  else
150
    if [[ $__IN_NAME == $__OUT_NAME.mp4 ]]
151
    then
152
      echo "Filenames can't be the same."
153
      exit
154
    fi
155
    __enc_mp4
156
  fi
157
}
158
159
__parse_args "${@}"
160
__main