Skip to content

Commit

Permalink
use aac_at encoder on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendonovich committed Jan 27, 2025
1 parent fa3c0e7 commit a1899c4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
2 changes: 1 addition & 1 deletion apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ async fn create_editor_instance(
let editor_instance = upsert_editor_instance(&app, video_id).await;

// Load the RecordingMeta to get the pretty name
let mut meta = RecordingMeta::load_for_project(&editor_instance.project_path)
let meta = RecordingMeta::load_for_project(&editor_instance.project_path)
.map_err(|e| format!("Failed to load recording meta: {}", e))?;

println!("Pretty name: {}", meta.pretty_name);
Expand Down
18 changes: 10 additions & 8 deletions apps/desktop/src/routes/(window-chrome)/setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,20 @@ function Startup(props: { onClose: () => void }) {
}
};

const handleStartupCompleted = () =>
generalSettingsStore.set({
hasCompletedStartup: true,
});

const handleGetStarted = async () => {
setIsExiting(true);
const currentWindow = getCurrentWindow();

// Cancel ongoing cloud animations
cloud1Animation?.cancel();
cloud2Animation?.cancel();
cloud3Animation?.cancel();

await generalSettingsStore.set({
hasCompletedStartup: true,
});
await handleStartupCompleted();

// Wait for animation to complete before showing new window and closing
setTimeout(async () => {
Expand Down Expand Up @@ -478,10 +480,10 @@ function Startup(props: { onClose: () => void }) {
<Button
class="px-12"
size="lg"
onClick={() => {
commands.openMainWindow().then(() => {
getCurrentWindow().close();
});
onClick={async () => {
handleStartupCompleted();
await commands.openMainWindow();
getCurrentWindow().close();
}}
>
Continue to Cap
Expand Down
3 changes: 2 additions & 1 deletion crates/media/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ impl AudioInfo {
fn channel_layout_raw(channels: u16) -> Option<ChannelLayout> {
Some(match channels {
1 => ChannelLayout::MONO,
_ => ChannelLayout::STEREO,
2 => ChannelLayout::STEREO,
_ => return None,
})
}

Expand Down
55 changes: 35 additions & 20 deletions crates/media/src/encoders/mp4.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ffi::CStr;

use crate::{
data::{
AudioInfo, FFAudio, FFPacket, FFRational, FFVideo, PlanarData, RawVideoFormat, VideoInfo,
Expand All @@ -13,6 +15,7 @@ use ffmpeg::{
threading::Config,
Dictionary, Rescale,
};
use ffmpeg_sys_next::FF_QP2LAMBDA;
use tracing::{debug, info, trace};

use super::Output;
Expand All @@ -38,8 +41,6 @@ pub struct MP4Encoder {
}

impl MP4Encoder {
const AUDIO_BITRATE: usize = 128 * 1000; // 128k

pub fn init(
tag: &'static str,
video_config: VideoInfo,
Expand Down Expand Up @@ -81,29 +82,44 @@ impl MP4Encoder {

let audio = if let Some(audio_config) = audio_config {
// Setup audio encoder
let audio_codec = encoder::find(ffmpeg::codec::Id::AAC)
.ok_or(MediaError::TaskLaunch("Could not find AAC codec".into()))?;
let audio_codec = encoder::find_by_name(if cfg!(target_os = "macos") {
"aac"
} else {
"aac_at"
})
.ok_or(MediaError::TaskLaunch("Could not find AAC codec".into()))?;
let mut audio_ctx = context::Context::new_with_codec(audio_codec);
audio_ctx.set_threading(Config::count(4));
let mut audio_enc = audio_ctx.encoder().audio()?;

if !audio_codec
.audio()
.unwrap()
.rates()
.into_iter()
.flatten()
.any(|r| r == audio_config.rate())
{
return Err(MediaError::TaskLaunch(format!(
"AAC Codec does not support sample rate {}",
audio_config.rate()
)));
}
let output_format = if cfg!(target_os = "macos") {
let output_format = ffmpeg::format::Sample::I16(format::sample::Type::Planar);

audio_enc.set_flags(ffmpeg::codec::Flags::QSCALE);
audio_enc.set_quality(10 * FF_QP2LAMBDA as usize);

output_format
} else {
audio_enc.set_bit_rate(128 * 1000);
let output_format = ffmpeg::format::Sample::F32(format::sample::Type::Planar);

if !audio_codec
.audio()
.unwrap()
.rates()
.into_iter()
.flatten()
.any(|r| r == audio_config.rate())
{
return Err(MediaError::TaskLaunch(format!(
"AAC Codec does not support sample rate {}",
audio_config.rate()
)));
}

let output_format = ffmpeg::format::Sample::F32(format::sample::Type::Planar);
output_format
};

audio_enc.set_bit_rate(Self::AUDIO_BITRATE);
audio_enc.set_rate(audio_config.rate());
audio_enc.set_format(output_format);
audio_enc.set_channel_layout(audio_config.channel_layout());
Expand Down Expand Up @@ -203,7 +219,6 @@ impl MP4Encoder {
// frame.samples()
// );

dbg!(audio.encoder.frame_size());
audio.buffer.consume(frame);

// Process all buffered frames
Expand Down
2 changes: 1 addition & 1 deletion crates/media/src/sources/screen_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use scap::{
use serde::{Deserialize, Serialize};
use specta::Type;
use std::{collections::HashMap, ops::ControlFlow};
use tracing::{error, info, instrument, trace, warn};
use tracing::{error, info, trace, warn};

use crate::{
data::{FFVideo, RawVideoFormat, VideoInfo},
Expand Down

1 comment on commit a1899c4

@vercel
Copy link

@vercel vercel bot commented on a1899c4 Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.