Specifying track title or language in MPEG DASH MANIFEST - ffmpeg

I am creating a manifest to playback Adaptive WebM using DASH. Everything working pretty fine but I need language-name/track-name instead of bitrate. Is it supported? How can update/optimize to support such feature?
Manifest creation:
ffmpeg \
-f webm_dash_manifest -i webm240.webm \
-f webm_dash_manifest -i webm360.webm \
-f webm_dash_manifest -i webm480.webm \
-f webm_dash_manifest -i webm720.webm \
-f webm_dash_manifest -i audio1.webm \
-f webm_dash_manifest -i audio2.webm \
-f webm_dash_manifest -i audio3.webm \
-f webm_dash_manifest -i audio4.webm \
-c copy -map 0 -map 1 -map 2 -map 3 -map 4 -map 5 -map 6 -map 7 \
-f webm_dash_manifest \
-adaptation_sets "id=0,streams=0,1,2,3 id=1,streams=4,5,6,7" \
manifest.mpd
Player audio track selection:

Finally, after changing a couple of DASH players and encoders, this is how I solved it.
The problem was not in manifest creation but in input file preparation. I added metadata to input files like below and it worked.
Tested in Shaka-player, works like charm.
ffmpeg -i input.mp4 -y -vn -acodec aac -ab 96k -dash 1 -metadata:s:a:0 language=hin audiohindi.mp4
ffmpeg -i input.mp4 -y -vn -acodec aac -ab 96k -dash 1 -metadata:s:a:0 language=tam audiotamil.mp4
ffmpeg -i input.mp4 -y -vn -acodec aac -ab 96k -dash 1 -metadata:s:a:0 language=kan audiokannada.mp4
ffmpeg -i input.mp4 -y -vn -acodec aac -ab 96k -dash 1 -metadata:s:a:0 language=tel audiotelugu.mp4
It uses ISO 639-2 language codes like: Wiki: ISO 639-2 language codes

Related

"Filter volume has an unconnected output" FFMPEG

I've looked at other posts about this but none of the flags seem to work with mine. Here is part of the bash script that runs the command and re-runs the script when there's an error, like when it disconnects.
cmd="ffmpeg \
-stream_loop -1 -re -thread_queue_size 256 -i "$VIDEO" \
-stream_loop -1 -re -thread_queue_size 256 -i "$AUDIO" \
-re -thread_queue_size 256 -i "$AUDIO" \
-filter_complex \
"[1:a]volume=0.0[a1]; \
[aout]" -map 0:v:0 -map "[aout]" \
-vcodec libx264 -pix_fmt yuv420p -preset $QUAL -r $FPS -g $(($FPS * 2)) -b:>
-acodec aac -ac 2 -ar 44100 -threads 0 -b:a $AB \
-f flv -rtmp_live live $URL/$KEY"
until $cmd ; do
echo "restarting ffmpeg command..."
sleep 2
done
Then it keeps restarting when I run the bash script with the error
Filter volume has an unconnected output

Disable default subtitle track with ffmpeg

I'm creating an MKV container with 4 different files:
video.mp4
audio_en.mp4
audio_es.mp4
subtitles.ass
For that I'm using the following ffmpeg script:
ffmpeg -i video.mp4 -i audio_es.mp4 -i audio_en.mp4 -i subtitles.ass \
-map 0:v -map 1:a -map 2:a -map 3:s \
-metadata:s:a:0 language=spa \
-metadata:s:a:1 language=eng \
-metadata:s:s:0 language=spa -disposition:s:0 -default \
-default -c:v copy -c:a copy -c:a copy -c:s copy result.mkv
The result.mkv looks awesome, everything works as expected except for one thing: subtitles are still set as the default track, so players like VLC shows them automatically. I've already tried plenty of different ways to avoid that to happen with the disposition flag but I cannot make it work.
How should I modify the script so that the MKV does not have the subtitles track marked as default?
Thanks in advance!
For Matroska (.mkv) output use the -default_mode option:
ffmpeg -i video.mp4 -i audio_es.mp4 -i audio_en.mp4 -i subtitles.ass \
-map 0:v -map 1:a -map 2:a -map 3:s \
-metadata:s:a:0 language=spa \
-metadata:s:a:1 language=eng \
-metadata:s:s:0 language=spa \
-default_mode infer_no_subs \
-c copy result.mkv
This option requires FFmpeg 4.3 or later, or use a build from the current git master branch.
When you don't want to update to FFmpeg 4.3 this option for deposit works for me:
-disposition:s:0 0
This option overrides the disposition copied from the input stream and delete it by setting it to 0.

How to optimize encoding and packaging videos using ffmpeg and shaka-packager

I'm trying to encode and package uploaded videos for an LMS website where video size may differ. How can I write a sh script that converts and packages the given video based on its size (For ex. if the given video resolution is bigger than 720p and less than 1080p FFmpeg should convert videos in 2 sizes [360p, 720p] then shaka-packager should package them).
So far I have this script assuming that input video resolution is 1080p (or 1080p <= size < 4k)
#!/bin/sh
pwd
URL="$1"
ID="$2"
FOLDER="$3"
if [ -z "$URL" ];then
echo "Must input a file"
$SHELL
exit
fi
DIR="$FOLDER/$ID"
OUTDIR="$DIR/cmaf"
mkdir -p -v $DIR
mkdir -p -v $OUTDIR
GOP_SIZE=50
FPS=25
CRF=28
INPUT="$DIR/input"
wget -c -O $INPUT $URL &&
if [ ! -f $FILE ]; then
echo "$FILE does not exists"
$SHELL
exit
fi
ffmpeg -i $INPUT -y \
-threads 1 \
-c:v libx264 -crf $CRF -profile:v high -pix_fmt yuv420p \
-keyint_min $GOP_SIZE -g $GOP_SIZE -sc_threshold 0 \
-color_primaries 1 -color_trc 1 -colorspace 1 -movflags +faststart \
-c:a aac -b:a 128k -ar 44100 \
-r $FPS \
"$DIR/input.mp4" &&
ffmpeg -i "$DIR/input.mp4" -y \
-threads 1 \
-vn -acodec copy "$DIR/a.mp4" \
-vf scale=640:360 -an "$DIR/360p.mp4" \
-vf scale=1280:720 -an "$DIR/720p.mp4" \
-vf scale=1920:1080 -an "$DIR/1080p.mp4" &&
rm -R $OUTDIR
packager \
in="$DIR/a.mp4",stream=audio,output="$OUTDIR/a.mp4",drm_label=AUDIO \
in="$DIR/360p.mp4",stream=video,output="$OUTDIR/360p.mp4",drm_label=SD \
in="$DIR/720p.mp4",stream=video,output="$OUTDIR/720p.mp4",drm_label=HD \
in="$DIR/1080p.mp4",stream=video,output="$OUTDIR/1080p.mp4",drm_label=HD \
--enable_raw_key_encryption \
--keys label=AUDIO:key_id=f3c5e0761e6654b28f8049c778b23947:key=a4637a153a443df9eed0593043db7517,label=SD:key_id=abba277e8bcf552bbd2e86a434a9a5d7:key=69eaa807a6763af979e8d1940fb88397,label=HD:key_id=6d76f25cb17f5e76b8eaef6b7f582d87:key=cb541784c99737aef4fff74500c12ea7 \
--pssh 000000377073776800000000EDEF8BA979D64ACEA3C877DCD51D21ED00000071220F7465737420636F6E74656E74206967 \
--mpd_output "$OUTDIR/h264.mpd" \
--hls_master_playlist_output "$OUTDIR/h264_master.m3u8"
The above script first downloads a video by a given URL then converts it to appropriate video format before resizing and packaging. I assumed if I convert the video before scaling would be more performant than every time converting and resizing it. Also, I assumed if I resize to all resolutions in one command it would be much faster, but I think that is not how FFmpeg works. I'm stack in the world of FFmpeg not knowing how to write sh(or bash) script better, cleaner and dynamic for encoding and packaging videos for online streaming. I think there are others with the same problem or the same case. So any help, fix and recommendation is appreciated
For the sake of clarity, I stripped some arguments from your commands (yuv420p and -profile:v high are defaults, not changing frame-rate)
ffmpeg -i <input> -y \
-c:v libx264 -crf 28 -g 50 \
-c:a aac -b:a 128k -ar 44100 \
-movflags +faststart \
<output> &&
ffmpeg -i <output> -y \
-vn -c:a copy "$DIR/a.mp4" \
-vf scale=640:360 -an "$DIR/360p.mp4" \
-vf scale=1280:720 -an "$DIR/720p.mp4" \
-vf scale=1920:1080 -an "$DIR/1080p.mp4"
The first run will decode your input and re-encode it using libx264 with quality-target 28 and a keyframe every 50 frames.
The second instance will decode it again, guessing an encoder by the .mp4 extension -- defaulting to libx264 --, and re-encodes everything three times by using the default values -g 250 -crf 23 (I'm not sure about -movflags +faststart).
So you are (1) overwriting your settings from the first-run, (2) having an additional decode process and (3) having a certain quality loss due to multiple lossy encodings.
What you want is to combine these into one invocation:
ffmpeg -i <input> -y \
-vn -c:a aac -b:a 128k -ar 44100 "$DIR/a.mp4" \
-c:v libx264 -crf 28 -g 50 -s 640x360 -movflags +faststart -an "$DIR/360p.mp4" \
-c:v libx264 -crf 28 -g 50 -s 1280x720 -movflags +faststart -an "$DIR/720p.mp4" \
-c:v libx264 -crf 28 -g 50 -s 19201080 -movflags +faststart -an "$DIR/1080p.mp4"
Additionally, I would stay away from special arguments unless you really know what and why you are choosing them.
P.s.
This is a command that runs with 15 % CPU utilization on my laptop.
ffmpeg \
-hwaccel qsv -c:v h264_qsv -i 'rtsp://109.98.78.106' \
-an -c:v h264_qsv -global_quality 30 -vf "scale_qsv=h=360:w=-1" "/tmp/360p.mp4" \
-an -c:v h264_qsv -global_quality 30 -vf "scale_qsv=h=720:w=-1" "/tmp/720p.mp4" \
-an -c:v h264_qsv -global_quality 30 -vf "scale_qsv=h=1080:w=-1" "/tmp/1080p.mp4"
It might have some color and / or quality issues but this is a performance trade-off.

ffmpeg combine audio mix code into complex concate script

I got currently 2 different ffmpeg scripts which I want to combine. I do not have good ffmpeg experience and those codes are mostly googel code so please be patient with me
The first code is concating 3 videos:
ffmpeg -y -i "$vid1" -i "$fp" -i "$vid1" -filter_complex \
"[0:v]scale=$cResolution:force_original_aspect_ratio=decrease,pad=$cResolution:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=30,format=yuv420p[v0]; \
[1:v]scale=$cResolution:force_original_aspect_ratio=decrease,pad=$cResolution:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=30,format=yuv420p[v1]; \
[2:v]scale=$cResolution:force_original_aspect_ratio=decrease,pad=$cResolution:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=30,format=yuv420p[v2]; \
[0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0]; \
[1:a]aformat=sample_rates=48000:channel_layouts=stereo[a1]; \
[2:a]aformat=sample_rates=48000:channel_layouts=stereo[a2]; \
[v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[v][a]; \
[v]drawtext=text='example..':y=h-line_h-$h3:x=w/30*mod(t\,20):enable='gt(mod(t,$dr2),$Introdr_rounded)'[v]; \
[v]drawtext=text='example..':y=h-line_h-$hcentral:x=w/20*mod(t\,100):enable='gt(mod(t,$dr2),$Introdr_rounded)'[v]; \
[v]drawtext=text='example..':y=h-line_h-23:x=w/30*mod(t\,20):enable='gt(mod(t,$dr2),$Introdr_rounded)'[v]" \
-map "[v]" -map "[a]" -c:v libx264 -crf 22 -preset veryfast -c:a aac -movflags +faststart "$fp_dest"
The second code is overlay a background mp3 in endless loop to the created video from above. Its important to know that this code does overlap the audio of the video and does not replace it. In future I will lower the volume of the mp3 files to work as background music
ffmpeg -y -i "$fp_dest" -filter_complex "amovie=$audio:loop=0,asetpts=N/SR/TB[aud];[0:a][aud]amix[a]" -map 0:v -map '[a]' -c:v copy -c:a aac -b:a 256k -shortest ./test.mp4
So currently I got 2 steps which I want to combine into 1 step. Can you please help me to include the second code into the first one without change any logic of the code?
Use amix to mix the music and the concated audio. stream_loop is applied to the music to loop it.
ffmpeg -y -i "$vid1" -i "$fp" -i "$vid1" -stream_loop -1 -i "$audio" -filter_complex \
"[0:v]scale=$cResolution:force_original_aspect_ratio=decrease,pad=$cResolution:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=30,format=yuv420p[v0]; \
[1:v]scale=$cResolution:force_original_aspect_ratio=decrease,pad=$cResolution:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=30,format=yuv420p[v1]; \
[2:v]scale=$cResolution:force_original_aspect_ratio=decrease,pad=$cResolution:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=30,format=yuv420p[v2]; \
[0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0]; \
[1:a]aformat=sample_rates=48000:channel_layouts=stereo[a1]; \
[2:a]aformat=sample_rates=48000:channel_layouts=stereo[a2]; \
[v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[v][a]; \
[a][3]amix=duration=first[a]; \
[v]drawtext=text='example..':y=h-line_h-$h3:x=w/30*mod(t\,20):enable='gt(mod(t,$dr2),$Introdr_rounded)'[v]; \
[v]drawtext=text='example..':y=h-line_h-$hcentral:x=w/20*mod(t\,100):enable='gt(mod(t,$dr2),$Introdr_rounded)'[v]; \
[v]drawtext=text='example..':y=h-line_h-23:x=w/30*mod(t\,20):enable='gt(mod(t,$dr2),$Introdr_rounded)'[v]" \
-map "[v]" -map "[a]" -c:v libx264 -crf 22 -preset veryfast -c:a aac -b:a 256k -movflags +faststart "$fp_dest"

FFmpeg add a text to last image only

I managed to create a video from set of non-sequential images and attached an audio to it. Also I added a "Copyright" text on top right hand corner so that the text appears throughout the video. However, I would like that text to appear only on the last image. How should I change my code below to address this?
ffmpeg \
-thread_queue_size 512 -f image2 -pattern_type glob -framerate 1/3 \
-i '*.jpg' \
-i 'audio.mp3' \
-c:a aac -c:v libx264 \
-vf scale=640:480, format=yuv420p, drawtext="text='Copyright':fontcolor=white:box=1:boxcolor=black#0.5:boxborderw=5:x=w-tw-5:y=5" \
-preset medium \
video.mp4
Isolate the last image from the glob and then concat it:
ffmpeg \
-pattern_type glob -framerate 1/3 -i '*.jpg' -framerate 1/3 -loop 1 -t 5 -i last/img.jpg -i audio.mp3 \
-filter_complex \
"[0:v]scale=640:480,setsar=1[v0]; \
[1:v]scale=640:480,setsar=1,drawtext=text='Copyright':fontcolor=white:box=1:boxcolor=black#0.5:boxborderw=5:x=w-tw-5:y=5[v1]; \
[v0][v1]concat=n=2:v=1:a=0,fps=25,format=yuv420p[v]" \
-map "[v]" -map 2:a -c:v libx264 -c:a aac -shortest -movflags +faststart video.mp4

Resources