Skip to content

Commit

Permalink
parse -ss and -t, closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 10, 2024
1 parent 2e35cde commit b379835
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ let read_command chan =
@@ String.(sub line (length "out_time=") (length "00:00:00")) )
| Some _ -> Nop

let read_commands chan filename duration =
let total = Float.to_int duration in
let read_commands chan filename total_duration partial_duration seek_to =
let total =
Float.to_int
@@
match (partial_duration, seek_to) with
| Some dur, _ -> parse_timestamp dur
| None, Some seek -> total_duration -. parse_timestamp seek
| None, None -> total_duration
in
let bar =
let open Progress.Line in
list
Expand All @@ -35,7 +42,7 @@ let read_commands chan filename duration =
prev := timestamp
done )

let read_output chan =
let read_output chan partial_duration seek_to =
let rec read_duration chan =
match In_channel.input_line chan with
| None -> Error "reached EOF before finding a Duration"
Expand All @@ -60,7 +67,7 @@ let read_output chan =
match
Result.bind (read_filename chan) (fun filename ->
Result.bind (read_duration chan) (fun duration ->
Ok (read_commands chan filename duration) ) )
Ok (read_commands chan filename duration partial_duration seek_to) ) )
with
| Error err -> failwith err
| Ok _ -> ()
Expand All @@ -75,20 +82,25 @@ let () =
; "$ ffmpeg -nostdin -stats -progress - -i input.mp4 out.mp4 2>&1 | ffbar"
]
in
let get_param flag args =
Option.bind
(Option.map (( + ) 1) (List.find_index (( = ) flag) args))
(List.nth_opt args)
in
match Sys.argv |> Array.to_list with
| [] | [_] ->
if Unix.(isatty stdin) then
print_endline usage
else
read_output stdin
read_output stdin None None
| _ :: args -> (
let chan =
Unix.open_process_in
@@ Filename.quote_command "ffmpeg"
(["-nostdin"; "-hide_banner"; "-stats"; "-progress"; "-"] @ args)
~stdout:"/dev/stdout" ~stderr:"/dev/stdout"
in
read_output chan ;
read_output chan (get_param "-ss" args) (get_param "-t" args) ;
match Unix.close_process_in chan with
| Unix.WEXITED 0 -> ()
| Unix.WEXITED n ->
Expand Down

0 comments on commit b379835

Please sign in to comment.