Score:2

Concatenating separate commands that download m4a and converts it to mp3 all in one

cz flag

I can download the audio using the command below but how can I also convert it into a mp3 file at the same time.

Example: Download m4a file

yt-dlp -f 139 --external-downloader aria2c --external-downloader-args '-d ./ -x 10' <url>

Convert the m4a file to mp3 using:

ffmpeg -i download_audio.m4a -codec:a libmp3lame -b:a 22050 -ac 1 -ar 22050 converted_audio.mp3

How can I convert these two commands into one? (so I don't have to execute two separate commands)

FedKad avatar
cn flag
The thing you are trying to avoid seems to be the intermediate file. If you cannot use normal pipes (IO redirection) in your commands, you can try _named pipes_.
Score:1
jp flag

It appears yt-dlp can write to STDOUT with the -o - option ... e.g. like so:

yt-dlp -o - -f 139 --external-downloader aria2c --external-downloader-args '-d ./ -x 10' URL_HERE

And ffmpeg can read from STDIN/pipe with -i pipe: or maybe even -i - ... e.g. like so:

... | ffmpeg -i pipe: -codec:a libmp3lame -b:a 22050 -ac 1 -ar 22050 converted_audio.mp3

So, you can try something like this:

yt-dlp -o - -f 139 --external-downloader aria2c --external-downloader-args '-d ./ -x 10' URL_HERE | ffmpeg -i pipe: -codec:a libmp3lame -b:a 22050 -ac 1 -ar 22050 converted_audio.mp3
Score:0
cz flag
yt-dlp -o - -f 139 --external-downloader aria2c --external-downloader-args '-d ./ -x 10' https://www.youtube.com/watch?v=2ZrWHtvSog4 | ffmpeg -i pipe: -codec:a libmp3lame -b:a 8000 -ac 1 -ar 8000 -f mp3 "$(basename "$(yt-dlp -e https://www.youtube.com/watch?v=2ZrWHtvSog4 | sed 's/[\/:*?"<>|]/_/g')").mp3"

To use the same output file name from the pipe as the input name with ffmpeg, you can modify the command as follows:

yt-dlp -o - -f 139 --external-downloader aria2c --external-downloader-args '-d ./ -x 10' https://www.youtube.com/watch?v=2ZrWHtvSog4 | ffmpeg -i pipe: -codec:a libmp3lame -b:a 8000 -ac 1 -ar 8000 -f mp3 "$(basename "$(yt-dlp -e https://www.youtube.com/watch?v=2ZrWHtvSog4 | sed 's/[\/:*?"<>|]/_/g')").mp3"

Explanation:

  1. The basename command is used to extract the base name from the URL. It retrieves the video title using yt-dlp -e https://www.youtube.com/watch?v=2ZrWHtvSog4 and then replaces any characters that are not allowed in file names with underscores using sed 's/[\/:*?"<>|]/_/g'.
  2. The $(...) syntax is used to execute a command and use its output as part of the larger command.
  3. $(basename "$(yt-dlp -e https://www.youtube.com/watch?v=2ZrWHtvSog4 | sed 's/[\/:*?"<>|]/_/g')").mp3 is the resulting file name with the .mp3 extension added.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.