Downloading and merging mp3 in time-range from Youtube using youtube-dl and ffmpeg

Background

I am trying to download best quality mp3 from Youtube videos in specified time range (first 30 seconds, 20-55 seconds etc), and then merge them to create one output file. Tools being used are youtube-dl, and fmpeg on Mac.

Step 1

youtube-dl command for downloading audio in time-range:

youtube-dl -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 <YoutubeUrl> --no-check-certificate --output "1.mp3" --external-downloader ffmpeg --external-downloader-args "-ss 00:00:00.00 -to 00:00:56.00"

Step 2

Output file names to input.txt:

file '1.mp3'
file '2.mp3'

Step 3

ffmpeg command to merge all mp3 files:

ffmpeg -f concat -safe 0 -i input.txt -c copy output.mp3 -c:a libmp3lame

Issue

Merging mp3 files is flashing below error:

[mp3 @ 0x7fdd4e812600] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

Questions

  1. How do I fix it? It seems there is some issue with encoding.
  2. Any recommendation on combining Step 2 and Step 3 into one command?

1 Answer

You can use concat filter to combine your steps 2 and 3 in one command:

ffmpeg -i 1.mp3 -i 2.mp3 -filter_complex "[0:a][1:a]concat=n=2:v=0:a=1[a]" -map "[a]" -c:a libmp3lame output.mp3

Let me know if this command fixes your other problem as well.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like