   defp extract_v1_metadata(path) do
     with {:ok, json} <- File.read(path),
          {:ok, %{"version" => 1} = attrs} <- decode_json(json) do
-      metadata = %{version: 1,
-                   terminal_columns: attrs["width"],
-                   terminal_lines: attrs["height"],
-                   terminal_type: get_in(attrs, ["env", "TERM"]),
-                   command: attrs["command"],
-                   duration: attrs["duration"],
-                   title: attrs["title"],
-                   shell: get_in(attrs, ["env", "SHELL"])}
+      metadata = %{
+        version: 1,
+        terminal_columns: attrs["width"],
+        terminal_lines: attrs["height"],
+        terminal_type: get_in(attrs, ["env", "TERM"]),
+        command: attrs["command"],
+        duration: attrs["duration"],
+        title: attrs["title"],
+        shell: get_in(attrs, ["env", "SHELL"])
+      }
+
       {:ok, metadata}
     else
       {:ok, %{"version" => version}} ->
         {:error, {:unsupported_format, version}}
+
       {:error, :invalid} ->
         {:error, :unknown_format}
     end

  defp to_relative_time(stream) do
    Stream.transform(stream, 0, &to_relative_time/2)
  end
  defp to_relative_time({t, s}, prev_time) do
    {[{t - prev_time, s}], t}
  end

    end
  end

  def create_asciicast(
        user,
        %{
          "meta" => meta,
          "stdout" => %Plug.Upload{} = data,
          "stdout_timing" => %Plug.Upload{} = timing
        },
        overrides
      ) do
    {:ok, attrs} = extract_metadata(meta)

    header = %{
      version: 2,
      width: attrs[:terminal_columns],
      height: attrs[:terminal_lines],
      title: attrs[:title],
      command: attrs[:command],
      env: %{"SHELL" => attrs[:shell], "TERM" => attrs[:terminal_type]}
    }

    header =
      header
      |> Enum.filter(fn {_k, v} -> v end)
      |> Enum.into(%{})

    overrides =
      if uname = attrs[:uname] do
        overrides
        |> Map.put(:uname, uname)
        |> Map.drop([:user_agent])
      else
        Map.put(overrides, :uname, uname)
      end

    {:ok, tmp_path} = Briefly.create()

