0

I have the following fish shell script for downloading a slice of a video using yt-dlp and ffmpeg

function yt-dlp-trim -d "Download a slice of a video with yt-dlp"

    if test (count $argv) -lt 3
        echo "Pass a start and end timestamp as well as an URL"
        return 1
    end
    
    # convert a hh:mm:ss.ms input into seconds with decimals
    set -l start (echo $argv[1] | sed -E 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' | bc)
    set -l end (echo $argv[2] | sed -E 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' | bc)
    
    # set output to $XDG_VIDEOS_DIR
    set -l video_dir (xdg-user-dir VIDEOS)
    set -l output $video_dir'/%(title)s.%(ext)s'

    command yt-dlp  -f '(bestvideo[height<=720]+bestaudio/best[height<=720]/best)[protocol!*=dash]'\
            --external-downloader ffmpeg\
            --external-downloader-args ffmpeg:"-ss $start -to $end"\
            -o $output\
            $argv[3..-1]
end

The important part is here:

command yt-dlp  -f '(bestvideo[height<=720]+bestaudio/best[height<=720]/best)[protocol!*=dash]'\
        --external-downloader ffmpeg\
        --external-downloader-args ffmpeg:"-ss $start -to $end"\
        -o $output\
        $argv[3..-1]

Where I run yt-dlp and specify ffmpeg as the external downloader.

The script does its job and works fine if I'm downloading from the start of a video until a certain point. However when I, say, download a video from 00:45 to 00:57 the result is a 12 second video with completely black or dead frames until the 4 second mark and then it proceeds normally until the end of the clip.

I've tried the advice from this answer by removing 30 seconds from $start and using this command

# go 30s back to get good keyframes
set start (math "max($start - 30, 0)")

command yt-dlp  -f '(bestvideo[height<=720]+bestaudio/best[height<=720]/best)[protocol!*=dash]'\
        --external-downloader ffmpeg\
        --external-downloader-args ffmpeg:"-ss $start -ss 30 -to $end"\
        -o $output\
        $argv[3..-1]

But it did not work. Any pointers would be appreciated.

0

You must log in to answer this question.

Browse other questions tagged .