Joining MP3's via commandline with background track - linux

I'm already taking advatage of two command line utilities. I'm using ffmpeg to convert m4a to mp3, and then I'm combining a few mp3's into one large one using mp3wrap. The resulting file is something like this:
BackgroundMusic.mp3 > Audio1.mp3 > Audio2.mp3
I need something more like
Audio1.mp3 > Audio2.mp3
|_____________________|
|
BackgroundMusic.mp3
To where the background music runs continuously in the background. Would be nice to be able to change the volume of each track too.
Does anyone know a command line program like mp3wrap but can also add in a background track?
I will not be able to use a GUI program such as Audacity, as all of this will be automated on the server.
Thanks!

You can do this with FFmpeg alone.
ffmpeg -i input_audio1 -i input_audio2 -i input_background_audio -filter_complex "
aevalsrc=0:d=10[s1];
[0:a]volume=volume=0.1[volume0];
[1:a]volume=volume=0.1[volume1];
[2:a]volume=volume=0.1[volume2];
[s1][volume1]concat=n=2:v=0:a=1[ac1];
[volume0][ac1]amix=inputs=2[amixed1];
[amixed1][volume2]amix=inputs=2:duration=first" output_audio
You need to use filter_complex to chain all the filters that you are going to use for adjusting the volumes, silent spaces, concat, etc. filters.
As the first step you can concatenate two audio files that you need to play one after the other. To do that I have first created and silent audio using aevalsrc filter with the same duration as the first audio clip. Then use concat filter to concatenate silent audio and the second audio.
To adjust the audio I have used volume filter. You can adjust the volume values accordingly. To mix the audios you can use amix filter. You can specify duration attribute to get the first input which is [amixed1] duration. With that option you can stop the whole audio with the duration of audio1+audio2 without play it for the full duration of background audio duration.
Hope this helps!

Related

Incorrect values for song length (duration) in Mp3tag after ffmpeg FLAC to MP3 conversion

The problem
As per this post, I use the following command to convert a flac file to mp3 while keeping all metadata:
ffmpeg -i input.flac -ab 320k -map_metadata 0 -id3v2_version 3 output.mp3
When inspecting the converted mp3 file by right-clicking it, going to properties and then details, everything looks in order. The value for "Length" is correct.
When inspecting the converted mp3 file with Mp3tag, the value for "Length" is different. From my testing, the "Length" value is consistently about 28% of what it is supposed to be.
Normally, this isn't an issue. Most music players I use, read the correct length value, same as Windows. However, I've recently discovered that Spotify Mobile for some reason ignores the length value that can be seen in the Windows panel and uses the one that can be seen in Mp3tag.
I want to figure out what command I should use so that after the flac file has been converted to mp3, Mp3tag shows the correct length, and there by, Spotify Mobile reads the correct length as well.
What I have tried
1.
After converting the file to Mp3, I've tried reencoding the mp3 file into a... mp3 file using the following command:
ffmpeg -i original.mp3 -c:v copy -c:a mp3 -vn -ar 44100 -ac 2 -b:a 320k copy.mp3
As can be seen in the image above, this fixes the issue and the length is showing correctly in Mp3tag and in Spotify Mobile.
Issues with this: Reencoding reduces quality and I don't know how to combine the previous flac conversion command and this one into one line.
2.
I tried https://cloudconvert.com/flac-to-mp3 and it worked. The length is displayed correctly in Mp3tag. (What commands did they use on the server???)
Issues with this: I don't want to rely on a cloud service for conversion, I have a lot of files to convert and I'd prefer it to be done locally.
Some demo files
Here is a folder with a flac file, a bad mp3 file (wrong length) and a good mp3 file. It looks like if you preview the music in google drive, it also plays the wrong length for the bad mp3 (39s not 2m19s), while vlc, groove player, spotify (desktop not mobile) all play the correct full length (2m19s) for the bad mp3 file.
Folder: here's the link
It seems I had an outdated version of ffmpeg... (ffmpeg version git-2020-05-23-26b4509) I updated to the latest version and the issue went away. Learned my lesson the hard way.
Would still appreciate an explanation on why this was happening. I'm curious. Why were there two values for length?

How do I split a M4B file provided I have a CUE file?

I have a large M4B file and a CUE file for it. I want to either split it into many M4B files, or split it into many MP3 files (former preferred).
I want to do this in the command line (OS X, but can use Linux if needed) and not install sketchy software. I have ffmpeg and can install other command line audio programs.
There is 2-step process which I did and it worked well.
Step 1 - Convert M4B to MP3
For this, you can use VLC Media Player (not sure how to do via command line so here are the GUI instructions).
Go to Media->Convert/Save
Add the M4B file, click "Save/Convert"
Under Profile, choose Audio-MP3
Select the destination (output) file
Press Start
...wait...but you should see a progress bar
Step 1.5 - Fix missing tags
Unfortunately, using VLC to convert the m4b to mp3 will remove all tags. So you might want to use something like puddletag (linux) to add the tags back in.
Step 2 - Split the MP3 using the .cue file
Once you have the MP3, use mp3splt (command-line)
The -c flag allows you to use the cue file for the split points.
Both of these are well worn programs (at least I have used both for years) so I do not think they are considered "sketchy."
I don't know how strict the CUE sheet format is, but assuming yours follows this general format:
TRACK 1 AUDIO
TITLE "Chapter 01"
INDEX 01 0:0:00
TRACK 2 AUDIO
TITLE "Chapter 02"
INDEX 01 22:56:01
then you can do this with ffmpeg and a perl one-liner.
user#host:~$ INFILE=input.m4b
user#host:~$ OUTPREFIX=output
user#host:~$ OUTEXT=mp3
user#host:~$ perl -ne 'sub p{printf(qq#"%s" -ss %f -to %f "%s_Chapter_%02d%s"\n#, "'"$INFILE"'", $_[0], $_[1], "'$OUTPREFIX'", $_[2], "'$OUTEXT'")}; if(/^\s+INDEX (\d+) (\d+):(\d+):(\d+)$/){$a=$b;$b=$4/60+$2*60+$3; p($a, $b, ++$c)if$b>0}; END{p($b, $b*100, ++$c)}' CUEFILE.cue | xargs -n 6 ffmpeg -i
The perl one-liner ignores all lines in the cue sheet but the INDEX ... lines and also ignores the first index, assuming it is zero. It prints out arguments for ffmpeg. xargs takes the right number of arguments (6 at a time, in this case) and calls ffmpeg once for each chapter (or track, or whatever).
This method can be used with a cue sheet to split up and convert any audio/video file provided ffmpeg accepts the input and output file formats.

Merging video to an audio file from in between or at a particular time

I am aiming to merge couple of media files.
So there is one audio file generated for say x minutes.And in between this duration multiple video files are generated at different timestamps (but all ends at or before that audio file ).
So I want to merge them such that different video files are merged with audio only at their respective times.
For eg -Following would be appearance of merged file, where A = audio stream and Vx = video streams
Start
2:00 A
2:07 A + V1
2:13 A + V2
2:17 A + V3
2:24 A
Finish
Should I use ffmpeg concatenation ? but ot sure it supports merging that starts at a particular time interval.
Thanks in advance !
I can use " -itsoffset " option in ffmpeg command to give offset for audio or video to start.
Credits:
How to combine a .mp4 video with a .wav audio with an offset in ffmpeg from command line?

MPlayer — changing brightness/ contrast to video file and save output

I need to change brightness and contrast to a video permanently, I tried this:
mplayer -vf eq=50:50 a.mp4 -dumpstream
mv stream.dump b.mp4
But it saves as a file which look likes the original file. Any idea?
You want to use mencoder to transcode the video to apply the video filter eq=50:50. When you use -dumpstream with mplayer, it simply dumps the stream while the video filter is being applied to playback. Take a look at the mencoder options, but you'll need to chose a video codec and some options for that codec (like bitrate). Then you can apply the brightness and contrast filter.

continously add picture to video

Every x minutes I grab an image from a network-cam. Now i want to add this picture to an existing video file - on the fly.
I don't want to keep numerous image files and then encode them once in a while with e.g.
mencoder mf://#${LIST} -mf type=jpg:fps=${FPS} ...
The video format/codec doesn't really matter, as long as standard tools (mplayer, ffmpeg, vlc, ...) can handle it.
Any ides or suggestions?
Thanks in advance!
One obvious way which should work (at least according to my first tests) is to just write the new jpeg image to the end of the video file - so the video is a mjpeg stream.
cat ${PIC} >> ${VIDEO}
This is an answer to my question, however i was looking for something consuming less space than the pictures stored each by its own would take up.
Other hints?

Resources