Skip to content

Commit

Permalink
Merge pull request #4 from jlvihv/forward
Browse files Browse the repository at this point in the history
forward
  • Loading branch information
jlvihv authored Jun 25, 2024
2 parents 5940e49 + bd196c4 commit d9487ba
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn execute_ffmpeg_command(ffmpeg_command: Vec<String>) -> Result<Child> {
);
return Err(anyhow::anyhow!(error_message));
}
println!("录制进程启动:{:?}", child.id());
println!("进程启动:{:?}", child.id());
Ok(child)
}

Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn run() {
manager::ffmpeg_api::execute_ffmpeg_command,
manager::ffmpeg_api::execute_ffmpeg_command_return_output,
manager::ffmpeg_api::get_image_info,
manager::ffmpeg_api::kill_child,
manager::request_api::request,
manager::request_api::try_request_get_status,
manager::request_api::request_post,
Expand Down
23 changes: 21 additions & 2 deletions src-tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use std::{path::PathBuf, process::Child};
// 用一个 dashmap 用来保存已经开始的录制任务
pub static TASKS: Lazy<DashMap<String, Child>> = Lazy::new(|| DashMap::new());

// 全局 dashmap,键是 id,值是 ffmpeg 进程
static CHILDS: Lazy<DashMap<u32, Child>> = Lazy::new(|| DashMap::new());

/// 内部方法,不对外暴露
pub mod inner {
use super::*;
Expand Down Expand Up @@ -417,9 +420,25 @@ pub mod ffmpeg_api {

/// 执行 ffmpeg 命令
#[tauri::command]
pub async fn execute_ffmpeg_command(ffmpeg_command: Vec<String>) -> Result<(), String> {
ffmpeg::execute_ffmpeg_command(ffmpeg_command)
pub async fn execute_ffmpeg_command(ffmpeg_command: Vec<String>) -> Result<u32, String> {
let child = ffmpeg::execute_ffmpeg_command(ffmpeg_command)
.map_err(|e| format!("Could not run ffmpeg command: {}", e))?;
let child_id = child.id();
CHILDS.insert(child.id(), child);
Ok(child_id)
}

/// 杀掉 ffmpeg 进程
#[tauri::command]
pub async fn kill_child(id: u32) -> Result<(), String> {
if let Some((_id, mut child)) = CHILDS.remove(&id) {
child
.kill()
.map_err(|e| format!("Could not kill child process: {}", e))?;
child
.wait()
.map_err(|e| format!("Could not wait for child process: {}", e))?;
}
Ok(())
}

Expand Down
10 changes: 5 additions & 5 deletions src/lib/components/sidebar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
href: '/record/plan'
},
// { label: $t('streamPush'), icon: 'icon-[fluent--video-28-regular]', href: '/live' },
// {
// label: $t('streamForward'),
// icon: 'icon-[fluent--square-arrow-forward-32-regular]',
// href: '/live/forward'
// },
{
label: $t('streamForward'),
icon: 'icon-[fluent--square-arrow-forward-32-regular]',
href: '/live/forward'
},
{ label: $t('settings'), icon: 'icon-[fluent--settings-32-regular]', href: '/config' }
];
</script>
Expand Down
5 changes: 3 additions & 2 deletions src/lib/i18n/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"planDeleteFailed": "删除计划失败",
"planAdded": "已添加计划",
"xiaohongshu": "小红书",
"streamForwardWarning": "警告:直播流转发是实验性功能,目前仅支持转发到小红书,且未来可能移除此功能。使用此功能可能导致您的平台账号被封禁,请谨慎使用。若您执意使用此功能,对您造成的任何损失,我概不负责。",
"streamForwardWarning": "警告:直播流转发是实验性功能,目前仅支持转发到小红书,且未来可能移除此功能。使用此功能可能导致您的平台账号被封禁,请谨慎使用。若您执意使用此功能,对您造成的任何损失,我概不负责。目前功能不完整,开始转发后,请不要切换页面,不然就找不回来了,您只能从任务管理器中结束 ffmpeg 进程。",
"startForward": "开始转发",
"streamPush": "直播推流",
"streamForward": "直播转发",
Expand Down Expand Up @@ -141,5 +141,6 @@
"parseError": "解析直播流地址错误,请刷新试试",
"useProxy": "使用代理录制",
"useProxyPlaceholder": "请输入代理地址",
"advancedOptions": "高级选项"
"advancedOptions": "高级选项",
"stopForward": "停止转发"
}
3 changes: 2 additions & 1 deletion src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@
"parseError": "Failed to parse live stream address, please refresh and try again",
"useProxy": "Use proxy to record",
"useProxyPlaceholder": "Please enter the proxy address",
"advancedOptions": "Advanced options"
"advancedOptions": "Advanced options",
"stopForward": "Stop forwarding"
}
7 changes: 5 additions & 2 deletions src/routes/live/forward/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// 关键信息变量
let url = $state('');
let id = $state(0);
const xiaohongshuPushServer = 'rtmp://live-push.xhscdn.com/live';
let liveInfo: LiveInfo | undefined = $state();
let errorMessage = $state('');
Expand Down Expand Up @@ -66,9 +67,10 @@
}
let ffmpegCommand = buildFFmpegForwardCommand(streamUrl, outputUrl);
try {
await invoke('execute_ffmpeg_command', { ffmpegCommand });
id = await invoke('execute_ffmpeg_command', { ffmpegCommand });
// 只要不异常,就认为成功了
forwarding = true;
toast.success('已经开始转发啦');
} catch (e) {
toast.error(e as string);
forwarding = false;
Expand All @@ -77,8 +79,9 @@
async function stopForward() {
try {
await invoke('stop_ffmpeg_command');
await invoke('kill_child', { id });
forwarding = false;
toast.success('已经停止转发啦');
} catch (e) {
toast.error(e as string);
}
Expand Down

0 comments on commit d9487ba

Please sign in to comment.