Audio conversion and filter application using ffmpeg with 1 command - ffmpeg

I have an mp3 file I need to convert to wav, and apply loudnorm filter afterwards. So far i've been doing it with 2 commands.
ffmpeg -i input.mp3 output.wav
and
ffmpeg -y -nostdin -i output.wav -filter_complex "[0:0]FILTER PARAMETER" -map_metadata 0 -map_metadata:s:a:0 0:s:a:0 -map_chapters 0 -map [norm0] -c:a pcm_s16le -ar 1500 -c:s copy out2.wav
Is there anyway to use 1 command instead of 2? I tried converting straight from mp3 to out2.wav by changing output.wav to input.mp3, but when the resulting file is compared with diff, they are different compared to the 2 step process.
I'm fairly new to ffmpeg, and have spent hours on the documentation without really knowing how to accomplish what i'm trying to do.

Related

FFMPEG converting MP3 to MP4 is adding blank space, how to fix Terminal command?

I'm creating MP4 files from MP3 files + an image. Searching here at StackOverflow I found a Terminal command that gets me really close:
ffmpeg -loop 1 -r 1 -i pic.jpg -i input.mp3 -c:a copy -shortest -c:v libx264 output.mp4
Works almost perfectly so long as input.mp3 and pic.jpg are in the same folder. The problem is:
It frequently ends up with about 20-40seconds of blank space at the end of the resulting MP4. I can manually chop it off, but I'd love to find out if there's a way to alter this command so that the resulting MP4 file is exactly the length of the input MP3 file.
I don't know the ffmpeg commands well and know just enough Terminal to be dangerous. So I'm hoping it's something obvious haha.
This command should work for you:
ffmpeg -i input.mp3 -loop 1 -i pic.jpg -shortest -c:a copy -c:v mjpeg output.mp4
You do not need to set -r 1 with just one image and -loop 1.
I think -c:v mjpeg is more suitable to encode jpg than -c:v libx264.

ffmpeg/bash - mapping code for several output

I use ffmpeg on a RaspberryPi3 (99% sure it is a pre-compiled version which I got from https://johnvansickle.com/ffmpeg/ as other methods to install failed for me)
Starting point:
Several multimedia files, each with a single video track, one or more audio tracks, and sometimes plenty subtitles.
My objective:
A bash script taking 3 inputs: respectively track number for video (mostly 0), track number for audio (mostly 1 but can be 2 or..) and track number for subtitles (mostly 2 but can also be 3 or 7 or..).
As output, I want:
a) a new video file with selected tracks (video, single audio which is re-encoded, single subtitle)
b) a new separate srt file with only selected subtitle.
My code was already working for a) but can't seem to get it to work for b)
See below. It does generate the video I want with right selection of tracks.
But its fails to write the right srt file: seems to always default to first subtitle track ?
Is my mapping incorrect, or is this a bash coding issue ?
The file is called Remux.sh, and called with arguments so for example:
./Remux.sh 0 1 5
#!/bin/bash
PATH="/mnt/USB1/Series/_Remux/"
for fullfile in "$PATH"*.mkv
do
newname="${fullfile%}_converted.mkv"
srtname="${fullfile%}_converted.srt"
/usr/local/bin/ffmpeg -i "${fullfile}" -map 0:$1 -map 0:$2 -map 0:$3 \
-c:v copy -c:s copy -c:a ac3 -b:a 640k "${newname}" \
-c:s copy "$srtname"
done
Edit: many thanks #llogan for the pointer ! it's now working with the mapping addition :)
Each output must have its own -map options. Otherwise, an output with no -map options will use the default stream selection behavior which chooses only 1 stream per stream type.
#!/bin/bash
PATH="/mnt/USB1/Series/_Remux/"
for fullfile in "$PATH"*.mkv
do
newname="${fullfile%}_converted.mkv"
srtname="${fullfile%}_converted.srt"
/usr/local/bin/ffmpeg -i "${fullfile}" -map 0:$1 -map 0:$2 -map 0:$3 \
-c:v copy -c:s copy -c:a ac3 -b:a 640k "${newname}" \
-map 0:$3 -c:s copy "$srtname"
done
You may find it helpful to use stream specifiers in your -map options, such as -map 0:s:1 for subtitle stream #2 (index starts at 0). See FFmpeg Wiki: Map.
Recommend you paste your script into shellcheck.net for additional Bash suggestions.

How to replace the video track in a video file with a still image?

I am trying to use ffmpeg to replace the video track in a video file with a still image. I tried some commands I got from other questions such as the one here
ffmpeg -i x.png -i orig.mp4 final.mp4
ffmpeg -r 1/5 -i x.png -r 30 -i orig.mp4 final.mp4
But these didn't work. I'm not sure which of these arguments are required or not. The output should be accepted by YouTube as a valid video - I was able to simply remove the video track, but apparently they don't let you upload a video without a video track.
You can try looping the still image like this:
ffmpeg -loop 1 -i x.png -i orig.mp4 final.mp4
Then you can tweak the encoding process by introducing the following quality parameters:
ffmpeg -loop 1 -i x.png -i orig.mp4 -crf 22 -preset slow final.mp4
they are described here.
If your colorspace gets rejected by YouTube you can try adding: -pix_fmt yuv420p.
Solution: A final solution is something like this:
Where -t 30 is an example duration of 30 seconds.
Using -c:a copy will directly copy the original audio without a new re-encoding (is faster).
ffmpeg -loop 1 -i x.png -i orig.mp4 -map 0 -map 1:a -c:v libx264 -pix_fmt yuv420p -crf 22 -preset slow -c:a copy -shortest final.mp4

concatenate audio files with an image

I am trying to concatenate multiple audio files and a single image into one video file using one command.
I have list of mp3 files and a playlist file (.m3u) in a direcotry.
I managed to do this but my solution is bad:
reading the playlist file and creating a new .txt in the ffmpeg required format
concatenating the audio files using the .txt into an .mp3
concatenating the large audio file and the static image into a video
This creates 2 unnecessary files that I have to delete.
I tried a different command
ffmpeg -loop 1 -framerate 1 -i myImage.jpg -i file1.mp3 -i file2.mp3 -i file3.mp3 -filter_complex '[0:0][1:0][2:0]concat=n=3:v=0:a=1' -tune stillimage -shortest output.mp4
however im getting a Error initializing complex filters.
Invalid argument error
Another kick in the nuts is that the system im working on has spaces in the folder names.
i tried using -i "concat:file1.mp3|file2.mp3|..." however i cannot use double quote marks to quote out the path so I get an invalid argument error.
Thank you very much for your help.
Method 1: concat demuxer
Make input.txt containing the following:
file 'file1.mp3'
file 'file2.mp3'
file 'file3.mp3'
Run ffmpeg:
ffmpeg -loop 1 -framerate 1 -i myImage.jpg -f concat -i input.txt -filter_complex "[0]scale='iw-mod(iw,2)':'ih-mod(ih,2)',format=yuv420p[v]" -map "[v]" -r 15 -tune stillimage -map 1:a -shortest -movflags +faststart output.mp4
All MP3 files being input to the concat demuxer must have the same channel layout and sample rate. If they do not then convert them using the -ac and -ar options so they are all the same.
Method 2: concat filter
Update: There seems to be a bug with -shortest not working with the concat filter (I keep forgetting about that). See the method above using the concat demuxer, or replace -shortest with -t. The value for -t should equal the total duration of all three MP3 files.
ffmpeg -loop 1 -framerate 1 -i myImage.jpg -i file1.mp3 -i file2.mp3 -i file3.mp3 -filter_complex "[0]scale='iw-mod(iw,2)':'ih-mod(ih,2)',format=yuv420p[v];[1:a][2:a][3:a]concat=n=3:v=0:a=1[a]" -map "[v]" -r 15 -map "[a]" -tune stillimage -shortest -movflags +faststart output.mp4
Option descriptions
scale filter makes image have even width and height which is required when outputting YUV 4:2:0 with libx264.
format filter sets chroma subsampling to 4:2:0, otherwise libx264 will try to limit subsampling, but most players can only handle 4:2:0.
concat filter is accepting file1.mp3, file2.mp3, and file3.mp3 as inputs. Your original command was trying to concat the video to the audio resulting in Invalid argument.
-map "[v]" chooses the video output from -filter_complex.
-r 15 sets output frame rate to 15 because most players can't handle 1 fps. This is faster than setting -framerate 15.
-map "[a]" chooses the audio output from -filter_complex.
-map 1:a chooses the audio from input #1 (the second input as counting starts from 0).
-movflags +faststart after encoding finishes this option moves some data from the end of the MP4 output file to the beginning. This allows playback to begin faster otherwise the complete file will have to be downloaded first.

ffmpeg dts_delta_threshold and aresample=async=1

I am using ffmpeg to encode livestreams for use in a tvheadend server. ffmpeg and hls discontinuities dont work, but ive fixed that using streamlink to read the hls stream then pipe that into ffmpeg.
Sometimes the audio has gaps in the live stream and the audio goes out of sync from that point on, I have managed to fix this using aresample=async=1. ffmpeg inserts silence for the gaps and audio stays synced.
Tvheadend doesnt like dts discontinuities and the stream will freeze whenever one is encountered. I have also fixed this with dts_delta_threshold 1. With this option the stream plays seamlessly without any freezes
Here is where my problem comes in when using dts_delta_threshold 1 the aresample command no longer works, I assume because there are no more gaps so it cant insert the silence. Ive tried various combinations and ordering of options.
Is there any way to apply the aresample=async=1 and also the dts_delta_threshold 1 command after.
This is my current command
streamlink -l warning --ringbuffer-size 64M --hls-timeout 100000000 --hls-live-restart hls://192.168.10.1/play/$1.$2.m3u8 best -O | \
ffmpeg -loglevel fatal -err_detect ignore_err \
-f mpegts -i - \
-filter_complex "eq=contrast=${3:-1.0}" \
-c:v libx264 -crf 18 -preset superfast -tune zerolatency -pix_fmt yuv420p -force_key_frames "expr:gte(t,n_forced*2)" \
-c:a aac -b:a 256k -ac 2 -af aresample=async=1 \
-metadata service_provider=$1 -metadata service_name="$1.$2" -f mpegts pipe:1
Ive tried putting the dts_delta_threshold before and after input, same thing audio goes out of sync if there is a gap in audio. Ive tried putting async 1 before input but that doesnt work either

Resources