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