Skip to content

Instantly share code, notes, and snippets.

@jpsamaroo
Created July 7, 2022 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpsamaroo/6fc18e4aefa979cec0b0ef7dd3fcf605 to your computer and use it in GitHub Desktop.
Save jpsamaroo/6fc18e4aefa979cec0b0ef7dd3fcf605 to your computer and use it in GitHub Desktop.
FFMPEG Cutting Script - in Julia
#!/usr/bin/env julia
function ts_to_s(ts::AbstractString)
secs = tryparse(Float64, ts)
if secs !== nothing
return secs
end
hours, mins, secs = parse.(Float64, split(ts, ':'))
return (hours * 60 * 60) + (mins * 60) + secs
end
file = popfirst!(ARGS)
file_out = "$(file)_edited.mkv"
cp(file, file_out; force=true)
cuts = []
while !isempty(ARGS)
splits = popfirst!(ARGS)
start, stop = split(splits, '|')
start_s = ts_to_s(start)
stop_s = ts_to_s(stop)
println("Cutting $file from $start to $stop ($start_s to $stop_s)")
push!(cuts, (start_s, stop_s))
end
expr = join(map(cut->"between(t,$(cut[1]),$(cut[2]))", cuts), '+')
run(`ffmpeg -i $file_out -vf "select='eq($expr,0)',setpts=N/FRAME_RATE/TB" -af "aselect='eq($expr,0)',asetpts=N/SR/TB" "$(file_out).tmp.mkv"`)
mv("$(file_out).tmp.mkv", file_out; force=true)
println("Done editing. Output file is at $file_out")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment