/useful/trunk-1

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