-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathffmpeg.nix
218 lines (210 loc) · 6.33 KB
/
ffmpeg.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# This module provides some shorthand `pog` helpers that simplify some ffmpeg workflows!
final: prev:
with prev;
rec {
scale = pog {
name = "scale";
description = "a quick and easy way to scale an image/video!";
arguments = [
{ name = "source"; }
];
flags = [
{
name = "horizontal";
short = "x";
description = "the number of pixels wide the image should scale to";
}
{
name = "vertical";
short = "y";
description = "the number of pixels high the image should scale to";
}
{
name = "output";
description = "the filename to output to [defaults to the current name with a tag]";
}
];
script = helpers: with helpers; ''
file="$1"
name=""
scale=""
${var.empty "file"} && die "you must specify a source file!" 1
${notFlag "horizontal"} && ${notFlag "vertical"} && die "you can only scale in 1 dimension!" 1
if ${notFlag "vertical"}; then
name="''${horizontal}x"
scale="$horizontal:-1"
else
name="''${vertical}y"
scale="-1:$vertical"
fi
debug "x=$horizontal y=$vertical scale=$scale"
${var.empty "output"} && output="''${file%.*}.$name.''${file##*.}"
${_.ffmpeg} -i "$file" -vf scale="$scale" "$output"
'';
};
flip = pog {
name = "flip";
description = "a quick and easy way to flip an image/video!";
arguments = [
{ name = "source"; }
];
flags = [
{
name = "horizontal";
short = "x";
description = "flip the source horizontally";
bool = true;
}
{
name = "vertical";
short = "y";
description = "flip the source vertically";
bool = true;
}
{
name = "output";
description = "the filename to output to [defaults to the current name with a tag]";
}
];
script = helpers: with helpers; ''
file="$1"
sep=""
${var.empty "file"} && die "you must specify a source file!" 1
${var.empty "horizontal"} && ${var.empty "vertical"} && die "you must specify at least one way to flip!" 1
${var.empty "output"} && output="''${file%.*}.flip.''${file##*.}"
${notFlag "horizontal"} && ${notFlag "vertical"} && sep=","
${_.ffmpeg} -i "$file" -filter:v "''${vertical:+vflip}''${sep}''${horizontal:+hflip}" -c:a copy "$output"
'';
};
cut_video = pog {
name = "cut_video";
description = "a quick and easy way to cut a video with ffmpeg!";
arguments = [
{ name = "source"; }
];
flags = [
{
name = "start";
description = "the start timestamp to cut from";
default = "0.0";
}
{
name = "end";
description = "the end timestamp to cut to";
}
{
name = "duration";
description = "the length of time to cut out. will override --end if passed";
}
{
name = "output";
description = "the filename to output to [defaults to the current name with a tag]";
}
];
script = helpers: with helpers; ''
file="$1"
${var.empty "file"} && die "you must specify a source file!" 1
${file.notExists "file"} && die "the file '$file' does not exist!" 2
${var.empty "end"} && ${var.empty "duration"} && die "you must specify an end (-e|--end) or duration (-d|--duration)!" 3
${var.empty "output"} && output="''${file%.*}.cut.''${file##*.}"
start_sec="$(echo "$start" | ${fn.ts_to_seconds})"
if ${notFlag "duration"}; then
end_sec="$(echo "$end" | ${fn.ts_to_seconds})"
else
end_sec="$(echo "$start_sec" "$duration" | ${fn.add})"
fi
${_.ffmpeg} -ss "$start_sec" -to "$end_sec" -i "$file" -c:v libx264 -c:a aac "$output"
'';
};
crop_video = pog {
name = "crop_video";
description = "a quick and easy way to crop a video with ffmpeg!";
arguments = [
{ name = "source"; }
];
shortDefaultFlags = false;
flags = [
{
name = "x";
short = "x";
description = "the x value to start the crop box at";
default = "0";
}
{
name = "y";
short = "y";
description = "the y value to start the crop box at";
default = "0";
}
{
name = "width";
description = "the width of the crop box";
default = "in_w";
}
{
name = "height";
description = "the height of the crop box";
default = "in_h";
}
{
name = "output";
description = "the filename to output to [defaults to the current name with a tag]";
}
];
script = helpers: with helpers; ''
file="$1"
${var.empty "file"} && die "you must specify a source file!" 1
${file.notExists "file"} && die "the file '$file' does not exist!" 2
${var.empty "output"} && output="''${file%.*}.crop.''${file##*.}"
${_.ffmpeg} -i "$file" -filter:v "crop=$width:$height:$x:$y" "$output"
'';
};
to_mp3 = pog {
name = "to_mp3";
description = "a quick and easy way to convert an audio file to mp3!";
arguments = [
{ name = "source"; }
];
flags = [
{
name = "output";
description = "the filename to output to [defaults to the current name with a tag]";
}
{
name = "quality";
description = "quality, 0 to 9 (lower is higher quality)";
default = "4";
}
];
script = helpers: with helpers; ''
# https://trac.ffmpeg.org/wiki/Encode/MP3
file="$1"
${var.empty "file"} && die "you must specify a source file!" 1
${var.empty "output"} && output="''${file%.*}.mp3"
${_.ffmpeg} -i "$file" -c:v copy -c:a libmp3lame -q:a "$quality" "$output"
'';
};
faxify = pog {
name = "faxify";
flags = [
{
name = "output";
description = "the filename to output to [defaults to the current name with a tag]";
}
];
runtimeInputs = with final; [ ghostscript ];
script = helpers: with helpers; ''
file="$1"
${var.empty "output"} && output="''${file%.*}.faxify.''${file##*.}"
${final.imagemagick}/bin/convert -density 130 "$file" -rotate 0.33 -attenuate 0.15 +noise Gaussian -colorspace Gray "$output"
'';
};
ffmpeg_pog_scripts = [
crop_video
cut_video
faxify
flip
scale
to_mp3
];
}