ffmpeg being inprecise when trimming mp3 files - audio

I want to use ffmpeg to trim some mp3s without re-encoding. The command I used was
ffmpeg -i "inputfile.mp3" -t 00:00:12.414 -c copy out.mp3
However, out.mp3 has a length of 12.460s, and when I load the file in Audacity I can see that it was cut at the wrong spot, and not at 12.414s.
Why is this? I googled a bit and tried some other commands like ffmpeg -i "inputfile.mp3" -ss 0 -to 00:00:12.414 -c copy out.mp3 (which interestingly results in a different length of 12.434s) but could never get the milliseconds to be cut right.
PS. I wasn't sure whether SO was the right place to ask since it isn't technically programming related, however most of the stuff I found on ffmpeg for trimming audio files were stackoverflow questions, e. g. ffmpeg trimming videos with millisecond precision

You can't trim MP3 (nor most lossy codec output) with that level of precision. An MP3 frame or so of padding is added during encoding. (See also: https://wiki.hydrogenaud.io/index.php?title=Gapless, and all the hacks required to make this work.)
If you need precision timing, use something uncompressed like PCM in WAV, or a lossless compression like FLAC.

On Linux you can use mp3splt:
mp3splt -f mp3file.mp3 from to -o output file format
Example:
mp3splt -f "/home/audio folder/test.mp3" 0.11.89 3.25.48 -o #f_trimmed
this will create a "/home/audio folder/test_trimmed.mp3"
For more info to the parameters, check the mp3splt man page here
On Windows you can use mp3DirectCut
mp3DirectCut has a GUI, but it also have command line support

Related

How to mimic Audacity's "truncate silence" with ffmpeg "silenceremove" filter

I want to remove completely silence parts from wav files with ffmpeg.
Input wav can be like :
I am using the following ffmpeg command to remove silence part ffmpeg -i input.wav -af silenceremove=stop_periods=-1:stop_duration=0.2:stop_threshold=-45dB output.wav because I understand from the doc that it will remove all silence parts longer than 0.2 s (silence being below -45dB).
But I get that where silence part has only been reduced to around 0.1 wheras I want it to be 0 (no remaining silence).
In Audacity I will use "truncate audio" filter and choose the above parameters to detect silence and in the action part I will choose to truncate to 0: .
This will yield to what I want (ie an audio with no silence part remaining):
Searching on the internet only lead me to what I already do.
So how can I reproduce the output I get from Audacity "Truncate Silence" filter with ffmpeg and remove all silence parts from audio ?
Edit: The output from silencedetect filter is correct: ffmpeg -i input.wav -af silencedetect=0.2:n=-45dB -f null - detects exactly what audacity detects.
Thanks in advance for your help
It looks like the equivalent command to AUdacity's truncate silence behaviour is the following (in bold what is added):
ffmpeg -i input.wav -af silenceremove=start_periods=1:stop_periods=-1:stop_duration=0.2:start_threshold=-45dB:stop_threshold=-45dB output.wav
I am not sure why adding those 2 parameters leads to the expected behaviour but it works although for some files silenceremove can remove more parts than Audacity / silencedetect detect.

midi to ogg - pipeline distortion

I am trying to convert midi files to ogg or mp3. Eventually this will happen on a linux webserver but currently I am using a Windows 7 machine. I am using timidity to convert the midi to wav and then either sox or ffmpeg to convert the wav to ogg/mp3.
When I use an intermediate file the process works fine (in the first line below timidity creates file.wav)
timidity.exe file.mid -Ow
sox.exe file.wav file.ogg
However, when I try to pipe the timidity output into sox (as below), the resulting file ogg is horribly distorted
timidity.exe file.mid -Ow -o - | sox.exe -t wav - file.ogg
and I get a warning
sox.exe WARN wav: Premature EOF on .wav input file
I also get the same distortion problem when I replace sox with ffmpeg (and the appropriate command line options), or when I replace ogg with mp3 as the output format.
So what am I doing wrong?
Thanks,
Chris
Regarding the warning itself, you're doing nothing wrong. You may also see a warning from timidity that reads something like
Warning: -: Illegal seek: Can't make valid header
What's happening there is explained in the timidity manual page:
If output is directed to a non-seekable file, or if TiMidity++ is interrupted before closing the file, the file header will contain 0xffffffff in the RIFF and data block length fields.
Note that RIFF is the encoding format commonly called by its file extension, .wav. When timidity writes a RIFF file, it doesn't know how long the file will be, so it writes some placeholder junk in the header and moves on to writing the data. When it finishes with the data, it knows how long the file is, so it goes back to the beginning of the file and writes over that junk in the header. When you write to a pipe, it has no way to go back and rewrite anything: the downstream program has to handle the placeholder junk. Also from the timidity manual page:
The popular sound conversion utility sox is able to read such malformed files, so you can pipe data directly to sox for on-the-fly conversion to other formats.
Thus, the message you mentioned. Sox is informing you that the chef prepared the file wrong BUT SOX IS HAPPY TO EAT IT ANYWAY BECAUSE SOX IS NOT PICKY. Sox is apparently passive-aggressive. Who knew?
You can ignore those warning messages, because now they are telling you something you already know. Or, you can use a raw format and explicitly tell timidity and sox how to play well with one another:
timidity file.midi -Or1Ssl -s44.1 -o- | sox -t raw -b 16 -e signed -r 44.1k -c 2 - file.ogg
As for the distortion, that may be caused in part by quirks in the audio libraries on the Windows system. I note that the pipeline in the question, sans .exe extensions, produces output with no notable distortion on a linux system. Using a well-defined raw format in the pipeline may also help with that issue.
Note that for Ogg output, you can now get that directly from timidity:
timidity file.midi -o file.ogg -Ov

how to merge any video to my intro using ffmpeg

I have a single intro video. I want to add the intro using ffmpeg or a similar program in the beggining of the users uploaded video (and yes I do need to merge them in one file, so it would be possible to download it later)
I`ve been searching internet and it suggests to convert both (intro and the other video) in to .mpg format.
OK, so far so good, but now when I try to join them together I get
[mpeg4 # 0x5547c60]Invalid and inefficient vfw-avi packed B frames detected
So I`m guessing it is because of something being different in both videos, like frame rate or size.
The worst thing is users are allowed to upload videos in almost any formats, also 240p-720p quality, so there is not one default size to convert the intro video into.
How could this be done?
Your intro video should match the resolution of the user videos, you should have as many intro-videos in as many resolutions as the user videos. Or convert all the user videos to a single resolution to match that of the intro video.Are you doing intro.mpg + user.mpg to combine the videos? Is this giving the above error?
Use ffmpeg:
ffmpeg -i 'concat:input1|input2' -codec copy output
or
ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv -filter_complex '[0:0] [0:1] [1:0] [1:1] [2:0] [2:1] concat=n=3:v=1:a=1 [v] [a]' -map '[v]' -map '[a]' output.mkv
or
$ cat mylist.txt
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
$ ffmpeg -f concat -i mylist.txt -c copy output
Source: Concatenate two mp4 files using ffmpeg

Delay audio for a few seconds at the start of audio files (using ffmpeg)

I have been trying to get lots of wav files delayed by 2 seconds at the start using ffmpeg. And so far, even though I have read the manual, I was not able to get it working. Here is my command:
for %%A in (*.wav) do (
ffmpeg -i "%%A" -itsoffset 00:00:02 "%%~NA"1.wav )
And nothing is being changed. Files are simply getting copied. I also tried the same with mp3 files. I also tried mkv and avi (to make sure it was not a container writing issue), but it gives the same result also.
Command is same here and here, but it does not work. Please, help.
You must put -itsoffset BEFORE you specify input. So:
ffmpeg -itsoffset 00:00:02 -i "%%A" "%%~NA"1.wav
Changing the input time offset like that isn't going to do anything noticeable for a single stream, it's meant for fixing out-of-sync issues between audio and video streams.
Do you want to tack on two seconds of silence at the start? If so, one simple way that'd work (although it may feel a bit hackish) is to simply tack on a 2 second WAV full of silence, before the actual input. This would be accomplished by simply adding another -i option before the actual file:
ffmpeg -i 2secsilence.wav -i "%%A" "%%~NA"1.wav
I know this question is over 9 months old, but I came across it and wanted to add some more information about '-itsoffset'. From the ffmpeg trouble ticket pages (https://ffmpeg.org/trac/ffmpeg/ticket/1349):
This command should display file1 content one second earlier than file2 content:
ffmpeg -itsoffset -1 -i file1.ts -i file2.ts -vcodec copy -acodec copy -map 0:0 -map 1:1 out.ts
1) What I see is that -itsoffset adds or subtracts from all the timestamps (both the video and audio streams) in a file. So this option is only going to be useful when remuxing from separate input files.
2) outfile has expected playback behavior with .ts and .mkv containers.
3) It does not work with .avi (no timestamps, so not a surprise)
4) It does not work with .mp4 container (a bug?)
And that is where this issue stands as of today.

Linux images to video, double digit problem

i have a list of images which im trying to convert to a video.
the images are the following:
t2.jpg
t3.jpg
t4.jpg
I can convert those three images into a video with:
ffmpeg -r 5 -i t%d.jpg -y -an video.avi
but if i turn the names to t20.jpg,t30.jpg and t40.jpg, it doesn't work anymore.
and changing %d to %02d doesn't make any difference.
What am I doing wrong?
The user interface around ffmpeg seems to be very bad.
Is there any other way to turn a list of images into a video?
Hope this helps
For creating a video from many images:
ffmpeg -f image2 -i foo-%03d.jpeg -r 12 -s WxH foo.avi
The syntax foo-%03d.jpeg specifies to use a decimal number composed of three digits padded with zeroes to express the sequence number. It is the same syntax supported by the C printf function, but only formats accepting a normal integer are suitable.
Source

Resources