File size: 23,417 Bytes
2156b71
5beeefc
2156b71
 
 
5beeefc
 
2156b71
 
 
 
 
5beeefc
 
 
 
 
2156b71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5beeefc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2156b71
 
 
5beeefc
2156b71
5beeefc
 
 
2156b71
 
5beeefc
2156b71
 
 
 
 
 
5beeefc
2156b71
 
 
 
 
 
5beeefc
 
2156b71
 
5beeefc
 
 
 
 
 
 
 
 
 
 
2156b71
 
5beeefc
2156b71
 
 
5beeefc
 
 
2156b71
 
 
 
5beeefc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2156b71
 
 
 
 
 
 
5beeefc
 
 
 
 
 
 
 
 
 
 
2156b71
 
 
 
 
 
 
 
 
 
 
5beeefc
 
 
 
 
 
 
 
 
 
 
2156b71
 
 
 
 
 
 
 
 
 
5beeefc
 
2156b71
 
5beeefc
 
 
 
 
 
 
 
 
 
 
2156b71
 
5beeefc
2156b71
 
 
 
 
 
5beeefc
2156b71
 
5beeefc
 
 
2156b71
 
 
 
 
 
5beeefc
 
 
 
2156b71
 
5beeefc
 
 
 
 
 
 
 
2156b71
 
5beeefc
 
 
2156b71
 
 
 
 
 
 
 
 
 
 
 
5beeefc
 
 
 
 
 
 
 
2156b71
 
5beeefc
 
 
2156b71
 
5beeefc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2156b71
 
 
5beeefc
 
 
2156b71
 
 
 
5beeefc
 
2156b71
 
 
 
 
 
 
 
 
 
 
 
5beeefc
 
 
 
 
2156b71
 
 
 
5beeefc
2156b71
 
 
 
 
5beeefc
 
2156b71
 
 
 
5beeefc
2156b71
 
 
 
 
5beeefc
 
2156b71
 
 
 
 
5beeefc
 
2156b71
5beeefc
 
 
 
 
 
 
 
 
 
 
 
 
 
2156b71
 
 
 
5beeefc
 
 
 
 
 
 
 
 
2156b71
5beeefc
 
 
 
 
 
 
 
2156b71
5beeefc
 
 
 
 
2156b71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5beeefc
2156b71
 
5beeefc
 
 
 
2156b71
 
 
 
 
 
 
 
5beeefc
 
2156b71
 
5beeefc
 
 
 
 
 
 
 
 
 
 
 
2156b71
 
5beeefc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2156b71
 
5beeefc
 
 
 
2156b71
 
 
 
5beeefc
 
 
 
 
 
 
 
 
 
 
 
 
2156b71
 
 
 
 
5beeefc
2156b71
5beeefc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2156b71
 
5beeefc
 
 
 
 
 
 
 
 
 
 
 
2156b71
5beeefc
 
2156b71
5beeefc
 
 
 
2156b71
 
 
5beeefc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
use tokio::process::Command;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader};
use fred::interfaces::KeysInterface;
use anyhow::{Result, anyhow};
use std::path::Path;
use std::process::Stdio;
use std::time::{Duration, Instant};
use tokio::fs;
use crate::storage::StorageManager;
use crate::models::{UserTier, TaskStatus};
use crate::queue::WeightedScheduler;

/// Redis key TTL for live encode progress (seconds).
const JOB_PROGRESS_TTL_SECS: i64 = 300;
/// Minimum gap between progress Redis writes.
const PROGRESS_REPORT_INTERVAL: Duration = Duration::from_secs(1);

pub struct TranscodingConfig {
    pub preset: String,
    pub max_height: i32,
    pub crf: String,
}

impl TranscodingConfig {
    pub fn for_tier(tier: &UserTier) -> Self {
        match tier {
            UserTier::Free => Self {
                preset: "ultrafast".to_string(),
                max_height: 720,
                crf: "26".to_string(), // Slightly lower quality for space/speed
            },
            UserTier::Pro => Self {
                preset: "medium".to_string(),
                max_height: 2160, // Support up to 4K
                crf: "23".to_string(), // Better quality
            },
        }
    }
}

fn job_progress_key(job_id: &str) -> String {
    format!("job:progress:{}", job_id)
}

/// Write live encode progress `0..=100` to `job:progress:{job_id}` (fred Redis client).
pub async fn set_job_progress(sched: &WeightedScheduler, job_id: &str, percent: u32) {
    let pct = percent.min(100);
    let _: () = sched
        .client()
        .set(
            job_progress_key(job_id),
            pct.to_string(),
            Some(fred::types::Expiration::EX(JOB_PROGRESS_TTL_SECS)),
            None,
            false,
        )
        .await
        .unwrap_or_default();
}

/// Remove progress key so failed jobs do not leave a stale percentage.
pub async fn clear_job_progress(sched: &WeightedScheduler, job_id: &str) {
    let _: () = sched
        .client()
        .del(job_progress_key(job_id))
        .await
        .unwrap_or_default();
}

pub async fn process(
    _video_id: String, // Platform ID
    video_path: &str, // Local filesystem path
    format: &str,
    tier: UserTier,
    skip_thumbnail: bool,
    scheduler: Option<WeightedScheduler>,
    task_id: String, // Processing Key (= job_id for progress Redis key)
) -> Result<()> {
    println!("Starting processing for task_id={} video_path={}", task_id, video_path);

    // Check if file exists
    if !Path::new(video_path).exists() {
        return Err(anyhow!("Video file not found at path: {}", video_path));
    }

    let config = TranscodingConfig::for_tier(&tier);

    // 1. Metadata extraction
    if let Some(ref sched) = scheduler {
        let status = TaskStatus {
            progress: 5,
            status: "processing".to_string(),
            message: "Analyzing video streams and metadata...".to_string(),
            cover_url: None,
            cover_source: None,
        };
        let status_json = serde_json::to_string(&status).unwrap();
        let _: () = sched
            .client()
            .set(
                format!("task:status:{}", task_id),
                status_json,
                Some(fred::types::Expiration::EX(86400)),
                None,
                false,
            )
            .await
            .unwrap_or_default();
    }
    println!("Extracting metadata for {}", video_path);
    let (width, height, has_audio, duration_secs) = match get_video_metadata(video_path).await {
        Ok(m) => m,
        Err(e) => {
            eprintln!("Metadata extraction failed for {}: {}", video_path, e);
            if let Some(ref sched) = scheduler {
                clear_job_progress(sched, &task_id).await;
            }
            return Err(e);
        }
    };
    let aspect_ratio = width / height;
    println!(
        "Metadata: width={} height={} has_audio={} duration={:.3}s aspect_ratio={}",
        width, height, has_audio, duration_secs, aspect_ratio
    );

    // ── Orientation validation ────────────────────────────────────────────────
    // Reject jobs whose declared format (home/flash) does not match actual pixel
    // dimensions.  Square-ish videos (aspect 0.9–1.1) always pass regardless of
    // declared type.  This catches client-side bypasses (devtools, direct API).
    {
        let ratio = width as f64 / height as f64;
        let is_squareish = ratio >= 0.9 && ratio <= 1.1;
        if !is_squareish {
            let mismatch = match format {
                "flash" => ratio > 1.1,  // declared flash but video is landscape
                "home" => ratio < 0.9,   // declared home but video is portrait
                _ => false,
            };
            if mismatch {
                let msg = format!(
                    "Video orientation doesn't match the selected format: \
                     video is {}x{} ({} {}) but format is \"{}\"",
                    width,
                    height,
                    if ratio > 1.1 { "landscape" } else { "portrait" },
                    ratio,
                    format,
                );
                eprintln!("{}", msg);
                if let Some(ref sched) = scheduler {
                    clear_job_progress(sched, &task_id).await;
                }
                return Err(anyhow!("{}", msg));
            }
        }
    }

    // 2. Prep Output
    let output_dir = format!("{}_hls", video_path);
    if !Path::new(&output_dir).exists() {
        fs::create_dir_all(&output_dir).await?;
    }

    // 3. Transcode + Thumbnail Concurrently
    let transcoding_fut = transcode_tiered(
        video_path,
        &output_dir,
        format,
        &config,
        scheduler.clone(),
        task_id.clone(),
        has_audio,
        &tier,
        duration_secs,
    );
    let v_path = video_path.to_string();
    let thumbnail_fut = async move {
        if !skip_thumbnail {
            generate_thumbnail(&v_path).await
        } else {
            Ok(())
        }
    };

    // Run both. Pro users might benefit from faster total completion time
    let (trans_res, thumb_res) = tokio::join!(transcoding_fut, thumbnail_fut);

    match (&trans_res, &thumb_res) {
        (Ok(()), Ok(())) => {}
        _ => {
            if let Some(ref sched) = scheduler {
                clear_job_progress(sched, &task_id).await;
            }
            trans_res?;
            thumb_res?;
        }
    }

    // 4. Upload to Storage
    match StorageManager::new().await {
        Ok(storage) => {
            let s3_prefix = format!("videos/{}", task_id);
            if let Some(ref sched) = scheduler {
                let status = TaskStatus {
                    progress: 90,
                    status: "processing".to_string(),
                    message: "Syncing video segments to edge nodes...".to_string(),
                    cover_url: None,
                    cover_source: None,
                };
                let status_json = serde_json::to_string(&status).unwrap();
                let _: () = sched
                    .client()
                    .set(
                        format!("task:status:{}", task_id),
                        status_json,
                        Some(fred::types::Expiration::EX(86400)),
                        None,
                        false,
                    )
                    .await
                    .unwrap_or_default();
            }
            storage.upload_hls_dir(&output_dir, &s3_prefix).await?;

            // Upload thumbnail too
            if !skip_thumbnail {
                let thumb_path = format!("{}.jpg", video_path);
                let thumb_key = format!("thumbnails/{}.jpg", task_id);
                storage.upload_file(Path::new(&thumb_path), &thumb_key).await?;
            }
        }
        Err(e) => {
            eprintln!("Storage Manager initialization failed: {}. Skipping upload.", e);
            if let Some(ref sched) = scheduler {
                clear_job_progress(sched, &task_id).await;
            }
            return Err(anyhow!("Storage initialization failed: {}", e));
        }
    }
    Ok(())
}

/// Probe width/height, audio presence, and total duration (seconds).
/// Duration comes from format context (`format=duration`), with stream duration as fallback β€”
/// equivalent to reading container duration / stream time_baseΓ—duration in ffmpeg-next.
async fn get_video_metadata(video_path: &str) -> Result<(i32, i32, bool, f64)> {
    let output = Command::new("ffprobe")
        .args(&[
            "-v",
            "error",
            "-select_streams",
            "v:0",
            "-show_entries",
            "stream=width,height",
            "-of",
            "csv=s=x:p=0",
            video_path,
        ])
        .output()
        .await?;

    let s = String::from_utf8(output.stdout)?;
    let parts: Vec<&str> = s.trim().split('x').collect();
    if parts.len() != 2 {
        return Err(anyhow!("Invalid ffprobe output: {}", s));
    }

    let width = parts[0].parse()?;
    let height = parts[1].parse()?;

    // Check for audio
    let audio_output = Command::new("ffprobe")
        .args(&[
            "-v",
            "error",
            "-select_streams",
            "a",
            "-show_entries",
            "stream=index",
            "-of",
            "csv=p=0",
            video_path,
        ])
        .output()
        .await?;

    let has_audio = !String::from_utf8(audio_output.stdout)?.trim().is_empty();

    let duration_secs = get_video_duration_secs(video_path).await.unwrap_or(0.0);

    Ok((width, height, has_audio, duration_secs))
}

async fn get_video_duration_secs(video_path: &str) -> Result<f64> {
    // Prefer container / format duration (same source as AVFormatContext.duration).
    let format_out = Command::new("ffprobe")
        .args(&[
            "-v",
            "error",
            "-show_entries",
            "format=duration",
            "-of",
            "default=noprint_wrappers=1:nokey=1",
            video_path,
        ])
        .output()
        .await?;

    let format_s = String::from_utf8_lossy(&format_out.stdout).trim().to_string();
    if let Ok(d) = format_s.parse::<f64>() {
        if d.is_finite() && d > 0.0 {
            return Ok(d);
        }
    }

    // Fallback: stream duration (seconds) β€” akin to stream.duration * av_q2d(time_base).
    let stream_out = Command::new("ffprobe")
        .args(&[
            "-v",
            "error",
            "-select_streams",
            "v:0",
            "-show_entries",
            "stream=duration",
            "-of",
            "default=noprint_wrappers=1:nokey=1",
            video_path,
        ])
        .output()
        .await?;

    let stream_s = String::from_utf8_lossy(&stream_out.stdout).trim().to_string();
    let d: f64 = stream_s
        .parse()
        .map_err(|_| anyhow!("Invalid duration from ffprobe: format={:?} stream={:?}", format_s, stream_s))?;
    if !d.is_finite() || d <= 0.0 {
        return Err(anyhow!("Non-positive duration: {}", d));
    }
    Ok(d)
}

async fn transcode_tiered(
    input: &str,
    output_dir: &str,
    format: &str,
    config: &TranscodingConfig,
    scheduler: Option<WeightedScheduler>,
    task_id: String,
    has_audio: bool,
    tier: &UserTier,
    duration_secs: f64,
) -> Result<()> {
    println!("Transcoding tiered levels for {} -> {}", input, output_dir);

    let source_height = get_video_height(input).await?;
    let _target_height = source_height.min(config.max_height);

    // ── 1. Determine variants ────────────────────────────────────────────────
    // num_variants = how many video streams (and therefore audio streams) we produce.
    // stream_map uses unique a:N per variant β€” sharing a:0 across variants is the
    // root cause of FFmpeg error "Same elementary stream found more than once".
    let (filter, stream_map, num_variants): (String, String, usize) = if format == "flash" {
        // Single quality for flash (Original resolution)
        let sm = if has_audio {
            "v:0,a:0,name:original".to_string()
        } else {
            "v:0,name:original".to_string()
        };
        (format!("[0:v]scale=w=-2:h={}[vout]", source_height), sm, 1)
    } else if config.max_height <= 720 {
        // Free tier β€” two quality levels, capped at 720p
        let sm = if has_audio {
            "v:0,a:0,name:720p v:1,a:1,name:480p".to_string() // ← unique a:0 / a:1
        } else {
            "v:0,name:720p v:1,name:480p".to_string()
        };
        (
            "[0:v]split=2[v1][v2]; [v1]scale=w=-2:h=720[v1out]; [v2]scale=w=-2:h=480[v2out]".to_string(),
            sm,
            2,
        )
    } else {
        // Pro tier β€” three quality levels up to 1080p
        let sm = if has_audio {
            "v:0,a:0,name:1080p v:1,a:1,name:720p v:2,a:2,name:480p".to_string() // ← unique a:0/a:1/a:2
        } else {
            "v:0,name:1080p v:1,name:720p v:2,name:480p".to_string()
        };
        (
            "[0:v]split=3[v1][v2][v3]; [v1]scale=w=-2:h=1080[v1out]; [v2]scale=w=-2:h=720[v2out]; [v3]scale=w=-2:h=480[v3out]".to_string(),
            sm,
            3,
        )
    };

    // ── 2. Build args as Vec<String> so we can push dynamic per-stream options ─
    let mut args: Vec<String> = vec![
        "-i".into(),
        input.into(),
        // NOTE: Do NOT add global -c:a here β€” it conflicts with per-stream -c:a:N below
        "-preset".into(),
        config.preset.clone(),
        "-crf".into(),
        config.crf.clone(),
        "-f".into(),
        "hls".into(),
        "-hls_time".into(),
        "6".into(),
        "-hls_playlist_type".into(),
        "vod".into(),
        "-master_pl_name".into(),
        "master.m3u8".into(),
        "-filter_complex".into(),
        filter.clone(),
    ];

    // ── 3. Video stream mappings ─────────────────────────────────────────────
    if filter.contains("split=3") {
        args.extend(
            [
                "-map", "[v1out]", "-c:v:0", "libx264", "-b:v:0", "5000k", "-map", "[v2out]",
                "-c:v:1", "libx264", "-b:v:1", "2800k", "-map", "[v3out]", "-c:v:2", "libx264",
                "-b:v:2", "1200k",
            ]
            .iter()
            .map(|s| s.to_string()),
        );
    } else if filter.contains("split=2") {
        args.extend(
            [
                "-map", "[v1out]", "-c:v:0", "libx264", "-b:v:0", "2800k", "-map", "[v2out]",
                "-c:v:1", "libx264", "-b:v:1", "1200k",
            ]
            .iter()
            .map(|s| s.to_string()),
        );
    } else {
        args.extend(
            ["-map", "[vout]", "-c:v:0", "libx264", "-b:v:0", "2800k"]
                .iter()
                .map(|s| s.to_string()),
        );
    }

    // ── 4. Audio stream mappings ─────────────────────────────────────────────
    // Each HLS variant needs its OWN audio output stream.  We map the single
    // source audio track N times (once per variant) so the muxer can write
    // independent AAC streams into the separate .ts segment files.
    if has_audio {
        for _ in 0..num_variants {
            args.push("-map".into());
            args.push("0:a?".into());
        }
        for i in 0..num_variants {
            args.push(format!("-c:a:{}", i));
            args.push("aac".into());
            args.push(format!("-b:a:{}", i));
            args.push("128k".into());
        }
    }

    // ── 5. HLS output options ────────────────────────────────────────────────
    let segment_filename = format!("{}/%v_%03d.ts", output_dir);
    let output_pattern = format!("{}/%v.m3u8", output_dir);

    args.extend([
        "-hls_segment_filename".to_string(),
        segment_filename,
        "-var_stream_map".to_string(),
        stream_map,
        output_pattern,
    ]);

    if let Some(ref sched) = scheduler {
        let status = TaskStatus {
            progress: 15,
            status: "processing".to_string(),
            message: format!("Compressing and optimizing video (Tier: {:?})", tier),
            cover_url: None,
            cover_source: None,
        };
        let status_json = serde_json::to_string(&status).unwrap();
        let _: () = sched
            .client()
            .set(
                format!("task:status:{}", task_id),
                status_json,
                Some(fred::types::Expiration::EX(86400)),
                None,
                false,
            )
            .await
            .unwrap_or_default();
        set_job_progress(sched, &task_id, 0).await;
    }

    run_ffmpeg_with_progress(&args, duration_secs, scheduler.as_ref(), &task_id).await
}

/// Spawn ffmpeg with `-progress pipe:1`, map `out_time` β†’ percent of input duration,
/// and throttle Redis writes to ~1/sec on `job:progress:{job_id}`.
///
/// This is the CLI equivalent of walking PTS in an ffmpeg-next encode loop:
/// `progress = (pts * time_base / total_duration) * 100`.
async fn run_ffmpeg_with_progress(
    args: &[String],
    duration_secs: f64,
    scheduler: Option<&WeightedScheduler>,
    task_id: &str,
) -> Result<()> {
    // Machine-readable progress on stdout; keep stderr for errors (drained so the pipe never blocks).
    let mut full_args: Vec<String> = vec![
        "-nostats".into(),
        "-progress".into(),
        "pipe:1".into(),
    ];
    full_args.extend(args.iter().cloned());

    let mut child = Command::new("ffmpeg")
        .args(&full_args)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()?;

    let stdout = child
        .stdout
        .take()
        .ok_or_else(|| anyhow!("ffmpeg stdout not captured"))?;
    let stderr = child
        .stderr
        .take()
        .ok_or_else(|| anyhow!("ffmpeg stderr not captured"))?;

    let stderr_task = tokio::spawn(async move {
        let mut buf = Vec::new();
        let mut reader = BufReader::new(stderr);
        let _ = reader.read_to_end(&mut buf).await;
        String::from_utf8_lossy(&buf).to_string()
    });

    let mut lines = BufReader::new(stdout).lines();
    let mut last_report = Instant::now()
        .checked_sub(PROGRESS_REPORT_INTERVAL)
        .unwrap_or_else(Instant::now);
    let mut current_out_us: u64 = 0;

    while let Some(line) = lines.next_line().await? {
        let line = line.trim();
        if let Some(v) = line.strip_prefix("out_time_us=") {
            if let Ok(us) = v.parse::<u64>() {
                current_out_us = us;
            }
        } else if let Some(v) = line.strip_prefix("out_time_ms=") {
            // Historical ffmpeg key: value is microseconds despite the name.
            if let Ok(us) = v.parse::<u64>() {
                current_out_us = us;
            }
        } else if line.starts_with("progress=") {
            let force = line == "progress=end";
            if force || last_report.elapsed() >= PROGRESS_REPORT_INTERVAL {
                if duration_secs > 0.0 {
                    let current_time_seconds = current_out_us as f64 / 1_000_000.0;
                    // Cap at 99 until process exit confirms success (then we set 100).
                    let progress_percent =
                        ((current_time_seconds / duration_secs) * 100.0).clamp(0.0, 99.0) as u32;
                    if let Some(sched) = scheduler {
                        set_job_progress(sched, task_id, progress_percent).await;
                    }
                }
                last_report = Instant::now();
            }
        }
    }

    let status = child.wait().await?;
    let err_text = stderr_task.await.unwrap_or_default();

    if !status.success() {
        eprintln!("FFmpeg Error: {}", err_text);
        if let Some(sched) = scheduler {
            clear_job_progress(sched, task_id).await;
        }
        return Err(anyhow!("FFmpeg failed: {}", err_text));
    }

    if let Some(sched) = scheduler {
        set_job_progress(sched, task_id, 100).await;
    }
    Ok(())
}

async fn get_video_height(input: &str) -> Result<i32> {
    let output = Command::new("ffprobe")
        .args(&[
            "-v",
            "error",
            "-select_streams",
            "v:0",
            "-show_entries",
            "stream=height",
            "-of",
            "csv=p=0",
            input,
        ])
        .output()
        .await?;
    let s = String::from_utf8(output.stdout)?;
    Ok(s.trim().parse()?)
}

async fn generate_thumbnail(video_path: &str) -> Result<()> {
    // Legacy path (thumbnails next to source). Prefer generate_and_upload_auto_cover.
    let thumb_path = format!("{}.jpg", video_path);
    extract_cover_frame(video_path, &thumb_path, 0.1).await
}

/// Seek ~`pct` into the video (default 10%) and write a single JPEG frame.
/// Uses the system ffmpeg CLI (same stack as the rest of this service; equivalent
/// to grabbing a decoded frame via ffmpeg-next around that timestamp).
pub async fn extract_cover_frame(video_path: &str, out_jpg: &str, pct: f64) -> Result<()> {
    let duration = match get_video_metadata(video_path).await {
        Ok((_, _, _, d)) if d.is_finite() && d > 0.0 => d,
        _ => 0.0,
    };
    let seek_secs = if duration > 0.5 {
        (duration * pct).clamp(0.0, (duration - 0.05).max(0.0))
    } else {
        0.0
    };
    let seek_arg = format!("{:.3}", seek_secs);

    println!(
        "Extracting cover frame from {} at {}s ({:.0}% of {:.2}s) β†’ {}",
        video_path,
        seek_arg,
        pct * 100.0,
        duration,
        out_jpg
    );

    let status = Command::new("ffmpeg")
        .args(&[
            "-ss",
            &seek_arg,
            "-i",
            video_path,
            "-frames:v",
            "1",
            "-update",
            "1",
            "-q:v",
            "2",
            "-y",
            out_jpg,
        ])
        .status()
        .await?;
    if !status.success() {
        return Err(anyhow!("Cover frame extraction failed"));
    }
    if !Path::new(out_jpg).exists() {
        return Err(anyhow!("Cover JPEG was not written: {}", out_jpg));
    }
    Ok(())
}

/// Auto cover: frame at ~10% duration β†’ JPEG β†’ S3 `covers/{task_id}.jpg`.
/// Returns the S3 key for inclusion in the Redis completion payload.
pub async fn generate_and_upload_auto_cover(
    video_path: &str,
    task_id: &str,
    storage: &StorageManager,
) -> Result<String> {
    let cover_local = format!("{}_cover.jpg", video_path);
    extract_cover_frame(video_path, &cover_local, 0.1).await?;

    let cover_key = format!("covers/{}.jpg", task_id);
    storage
        .upload_file(Path::new(&cover_local), &cover_key)
        .await?;

    // Best-effort cleanup of local JPEG
    let _ = fs::remove_file(&cover_local).await;

    println!("Auto cover uploaded: {}", cover_key);
    Ok(cover_key)
}