Skip to content

Commit

Permalink
use ffmpeg to generate peaks
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Jun 21, 2022
1 parent 1d844f2 commit 2854bfa
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 29 deletions.
21 changes: 1 addition & 20 deletions Aptfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,4 @@ libmad0-dev
libid3tag0-dev
libsndfile1-dev
libgd-dev
lame

# libboost-dev=1.65.1
# libboost-regex-dev=1.65.1
# libboost-filesystem-dev=1.65.1
# libboost-program-options-dev=1.65.1

libboost-dev
libboost-filesystem-dev
libboost-program-options-dev
libboost-regex-dev
# # http://mirrors.kernel.org/ubuntu/pool/main/b/boost-defaults/libboost-dev_1.65.1.0ubuntu1_amd64.deb

# http://mirrors.kernel.org/ubuntu/pool/universe/b/boost-defaults/libboost-regex-dev_1.65.1.0ubuntu1_amd64.deb

# http://mirrors.kernel.org/ubuntu/pool/main/b/boost-defaults/libboost-filesystem-dev_1.65.1.0ubuntu1_amd64.deb

# http://mirrors.kernel.org/ubuntu/pool/main/b/boost1.65.1/libboost-program-options1.65-dev_1.65.1+dfsg-0ubuntu5_amd64.deb

https://launchpad.net/~chris-needham/+archive/ubuntu/ppa/+files/audiowaveform_1.6.0-1bionic1_amd64.deb
lame
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ FROM elixir:1.13.4

RUN apt update
RUN apt install -y nodejs npm
RUN npm install --global yarn
RUN apt install -y inotify-tools

RUN mix local.hex --force
RUN apt-get install -y ffmpeg

RUN apt-get install software-properties-common -y
RUN add-apt-repository ppa:chris-needham/ppa
RUN apt-get update
RUN apt-get install audiowaveform



RUN npm install --global yarn

WORKDIR /app
2 changes: 1 addition & 1 deletion assets/js/controllers/audio_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class extends Controller {
pixelRatio: 10,
cursorWidth: 1,
cursorColor: "lightgray",
normalize: true,
normalize: false,
responsive: true,
fillParent: true
})
Expand Down
61 changes: 61 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
version: '2.4'

x-app:
&app
build:
context: .
dockerfile: ./Dockerfile
args:
PG_MAJOR: '14'
NODE_MAJOR: '16'
YARN_VERSION: '1.13.0'
environment:
&env
MIX_ENV: development
# image: chaskiq/chaskiq:1.1.0
tmpfs:
- /tmp

x-backend:
&backend
<<: *app
stdin_open: true
tty: true
volumes:
- .:/app:cached
environment:
<<: *env
DATABASE_URL: postgres://postgres:password@postgres:5432
EDITOR: vi
depends_on:
postgres:
condition: service_healthy

services:
runner:
<<: *backend
command: /bin/bash

phoenix:
<<: *backend
command: mix phx.server
ports:
- '4000:4000'

postgres:
image: postgres:14-alpine
volumes:
- ~/apps/postgres:/var/lib/postgresql/data
environment:
#POSTGRES_USER: postgresql
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
PSQL_HISTFILE: /root/log/.psql_history
ports:
- 5432
healthcheck:
test: pg_isready -U postgres -h 127.0.0.1
interval: 5s

volumes:
postgres: null
9 changes: 6 additions & 3 deletions lib/rauversion/services/peaks_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ defmodule Rauversion.Services.PeaksGenerator do
# audiowaveform -i /root/audio.mp3 --pixels-per-second 100 --output-format json -
end

def run_ffprobe(file) do
def run_ffprobe(file, duration) do
IO.inspect("processing #{file}")

pixels_per_frame = duration * 75

cmd =
"#{ffprobe_path()} -v error -f lavfi -i amovie=#{file},astats=metadata=1:reset=1 -show_entries frame_tags=lavfi.astats.Overall.Peak_level -of json"
"#{ffprobe_path()} -v error -f lavfi -i amovie=#{file},asetnsamples=n=#{pixels_per_frame}:p=0,astats=metadata=1:reset=1 -show_entries frame_tags=lavfi.astats.Overall.Peak_level -of json"
|> to_charlist()

IO.inspect(cmd)
Expand All @@ -46,6 +48,7 @@ defmodule Rauversion.Services.PeaksGenerator do
_ -> nil
end
end)
|> normalize
end

def desired_pixels_per_second(desired_pixels, duration) do
Expand Down Expand Up @@ -75,7 +78,7 @@ defmodule Rauversion.Services.PeaksGenerator do
input,
&((new_min + (&1 - min) / (max - min) * (new_max - new_min))
|> Decimal.from_float()
|> Decimal.round(4)
|> Decimal.round(1)
|> Decimal.to_float())
)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rauversion/tracks/track.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule Rauversion.Tracks.Track do
if kind == "audio" do
# coput temp file from live
{:ok, dir_path} = Temp.mkdir("my-dir")
new_path = "#{dir_path}#{file.filename}"
new_path = "#{dir_path}#{file.filename}" |> String.replace(~r/\s+/, "-")
:ok = File.cp(file.path, new_path)

file = %{file | path: new_path}
Expand All @@ -99,7 +99,7 @@ defmodule Rauversion.Tracks.Track do

duration = blob |> ActiveStorage.Blob.metadata() |> Map.get("duration")

case Rauversion.Services.PeaksGenerator.run_audiowaveform(path, duration) do
case Rauversion.Services.PeaksGenerator.run_ffprobe(path, duration) do
[_ | _] = data ->
put_change(struct, :metadata, %{peaks: data})

Expand Down
18 changes: 16 additions & 2 deletions test/rauversion/peak_genenerator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@ defmodule Rauversion.PeaksGeneratorTest do

describe "peaks generator" do
test "peaks gen" do
assert [hd | rest] = Rauversion.Services.PeaksGenerator.run("./test/files/audio.mp3")
assert [hd | rest] =
Rauversion.Services.PeaksGenerator.run_audiowaveform("./test/files/audio.mp3", 3)

IO.inspect(rest)
IO.inspect("qty: #{rest |> length}")
end

test "peaks gen ffprobe" do
a = Rauversion.Services.PeaksGenerator.run_ffprobe("./test/files/audio.mp3")
a = Rauversion.Services.PeaksGenerator.run_ffprobe("./test/files/audio.mp3", 3)
require IEx
IEx.pry()
end

test "desired pixels" do
desired_pixels = 500
duration = 3600

pixels_per_second =
Rauversion.Services.PeaksGenerator.desired_pixels_per_second(desired_pixels, duration)

require IEx
IEx.pry()
end
Expand Down

0 comments on commit 2854bfa

Please sign in to comment.