Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/transcribe from url #300

Merged
merged 8 commits into from
Sep 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
working download on unix
  • Loading branch information
thewh1teagle committed Sep 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 8ee9e93590f34d495f25f46215ea5d0fb8977838
1 change: 1 addition & 0 deletions desktop/src-tauri/src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ use tokio::sync::Mutex;
use vibe_core::transcript::Segment;
use vibe_core::transcript::Transcript;
pub mod audio;
pub mod ytdlp;

/// Return true if there's internet connection
/// timeout in ms
66 changes: 66 additions & 0 deletions desktop/src-tauri/src/cmd/ytdlp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::os::unix::fs::PermissionsExt;

use eyre::{bail, Context, Result};
use tauri::{AppHandle, Manager};

use super::get_ffmpeg_path;

fn get_binary_name() -> &'static str {
if cfg!(windows) {
"yt-dlp_windows.exe"
} else if cfg!(target_os = "linux") {
"yt-dlp_linux"
} else {
"yt-dlp_macos"
}
}

#[tauri::command]
pub fn get_temp_path(app_handle: AppHandle, ext: String, in_documents: Option<bool>) -> String {
let mut base_path = if in_documents.unwrap_or_default() {
app_handle.path().document_dir().unwrap()
} else {
std::env::temp_dir()
};
base_path.push(format!("temp_file.{}", ext));
base_path.to_string_lossy().to_string()
}

#[tauri::command]
pub async fn download_audio(app_handle: AppHandle, url: String, out_path: String) -> Result<String> {
tracing::debug!("download audio {}", url);
let name = get_binary_name();
let path = app_handle.path().app_local_data_dir().context("Can't get data directory")?;
let path = path.join(name);
let ffmpeg_path = get_ffmpeg_path();

// Set permission
#[cfg(unix)]
{
let meta = std::fs::metadata(path.clone())?;
let mut perm = meta.permissions();
// chmod +x
perm.set_mode(0o755);
std::fs::set_permissions(path.clone(), perm)?;
}

let output = std::process::Command::new(path)
.args([
"--no-playlist",
"-x",
"--audio-format",
"m4a",
"--ffmpeg-location",
&ffmpeg_path,
&url,
"-o",
&out_path,
])
.output()?;
if !output.status.success() {
let stderr = String::from_utf8(output.stderr)?;
bail!("Failed to download audio: {}", stderr);
}
let output = String::from_utf8(output.stdout)?;
Ok(output)
}
2 changes: 2 additions & 0 deletions desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -83,6 +83,8 @@ fn main() -> Result<()> {
cmd::check_vulkan,
cmd::get_logs_folder,
cmd::get_ffmpeg_path,
cmd::ytdlp::download_audio,
cmd::ytdlp::get_temp_path,
#[cfg(windows)]
cmd::set_high_gpu_preference
])
8 changes: 5 additions & 3 deletions desktop/src/lib/ytdlp.ts
Original file line number Diff line number Diff line change
@@ -25,8 +25,10 @@ export async function downloadYtDlp() {
await invoke('download_file', { url, path: binaryPath })
}

export async function downloadAudio(url: string) {
const ffmpegPath = await invoke<string>('get_ffmpeg_path')
let result = await Command.create('$APPLOCALDATA/yt-dlp_macos', ['--no-playlist', '-x', '--ffmpeg-location', ffmpegPath, url]).execute()
export async function downloadAudio(url: string, inDocuments?: boolean) {
const outPath = await invoke<string>('get_temp_path', { ext: 'm4a', inDocuments })
console.log('outPath is ', outPath)
let result = await invoke<string>('download_audio', { url, outPath })
console.log(result)
return outPath
}
24 changes: 21 additions & 3 deletions desktop/src/pages/home/Page.tsx
Original file line number Diff line number Diff line change
@@ -140,9 +140,27 @@ export default function Home() {
placeholder="https://www.youtube.com/watch?v=aj8-ABRl1Jo"
onKeyDown={(event) => (event.key === 'Enter' ? vm.downloadAudio() : null)}
/>
<button onMouseDown={vm.downloadAudio} className="btn btn-primary mt-0">
{t('common.download-file')}
</button>

{vm.downloadingAudio ? (
<button className="btn relative btn-success mt-3">
<span className="loading loading-spinner"></span>
</button>
) : (
<>
<label className="label cursor-pointer mt-2 mb-5">
<span className="label-text">{t('common.save-record-in-documents-folder')}</span>
<input
type="checkbox"
className="toggle toggle-primary"
onChange={(e) => vm.preference.setStoreRecordInDocuments(e.target.checked)}
checked={vm.preference.storeRecordInDocuments}
/>
</label>
<button onMouseDown={vm.downloadAudio} className="btn btn-primary mt-0">
{t('common.download-file')}
</button>
</>
)}
</div>
</div>
)}
15 changes: 14 additions & 1 deletion desktop/src/pages/home/viewModel.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import { ModelOptions, usePreferenceProvider } from '~/providers/Preference'
import { UpdaterContext } from '~/providers/Updater'
import * as ytDlp from '~/lib/ytdlp'
import { useTranslation } from 'react-i18next'
import { useToastProvider } from '~/providers/Toast'

export interface BatchOptions {
files: NamedPath[]
@@ -40,13 +41,15 @@ export function viewModel() {
const [audio, setAudio] = useState<HTMLAudioElement | null>(null)
const [progress, setProgress] = useState<number | null>(0)
const { t } = useTranslation()
const toast = useToastProvider()

const { files, setFiles } = useFilesContext()
const preference = usePreferenceProvider()
const [devices, setDevices] = useState<AudioDevice[]>([])
const [inputDevice, setInputDevice] = useState<AudioDevice | null>(null)
const [outputDevice, setOutputDevice] = useState<AudioDevice | null>(null)
const [audioUrl, setAudioUrl] = useState<string>('')
const [downloadingAudio, setDownloadingAudio] = useState(false)

const { updateApp, availableUpdate } = useContext(UpdaterContext)
const { setState: setErrorModal } = useContext(ErrorModalContext)
@@ -70,7 +73,11 @@ export function viewModel() {
okLabel: t('common.install-now'),
})
if (shouldInstall) {
toast.setMessage(t('common.downloading-ytdlp'))
toast.setOpen(true)
await ytDlp.downloadYtDlp()
toast.setOpen(false)
preference.setHomeTabIndex(2)
}
} else {
preference.setHomeTabIndex(2)
@@ -292,7 +299,11 @@ export function viewModel() {

async function downloadAudio() {
if (audioUrl) {
ytDlp.downloadAudio(audioUrl)
setDownloadingAudio(true)
const outPath = await ytDlp.downloadAudio(audioUrl, preference.storeRecordInDocuments)
preference.setHomeTabIndex(1)
setFiles([{ name: 'audio.m4a', path: outPath }])
setDownloadingAudio(false)
}
}

@@ -329,5 +340,7 @@ export function viewModel() {
audioUrl,
setAudioUrl,
downloadAudio,
downloadingAudio,
setDownloadingAudio,
}
}