I want to play all the audios at a designated time frame using this command tho they all play at the beginning of the video here is the command
ffmpeg -y -i ./temp/background.mp4 -itsoffset 1 -i ./temp/tts/0.mp3 -itsoffset 3 -i ./temp/tts/1.mp3 -itsoffset 4 -i ./temp/tts/10.mp3 -itsoffset 7 -i ./temp/tts/11.mp3 -itsoffset 8 -i ./temp/tts/12.mp3 -itsoffset 10 -i ./temp/tts/13.mp3 -itsoffset 12 -i ./temp/tts/14.mp3 -itsoffset 14 -i ./temp/tts/2.mp3 -itsoffset 15 -i ./temp/tts/3.mp3 -itsoffset 18 -i ./temp/tts/4.mp3 -itsoffset 20 -i ./temp/tts/5.mp3 -itsoffset 22 -i ./temp/tts/6.mp3 -itsoffset 23 -i ./temp/tts/7.mp3 -itsoffset 25 -i ./temp/tts/8.mp3 -itsoffset 27 -i ./temp/tts/9.mp3 -filter_complex amix=inputs=5[a] -map 0:v -map [a] -c:v copy -async 1 -c:a aac ./temp/withoutsubs.mp4
I was expecting all the audios to play at their right time
amix doesn't sync by timestamp. You have to pad the head of each mp3 to start from timestamp 0 like this,
ffmpeg -y \
-i ./temp/background.mp4 \
-itsoffset 1 -i ./temp/tts/0.mp3 \
-itsoffset 3 -i ./temp/tts/1.mp3 \
-itsoffset 4 -i ./temp/tts/2.mp3 \
-filter_complex \
"[1]aresample=async=1:first_pts=0[1a];
[2]aresample=async=1:first_pts=0[2a];
[3]aresample=async=1:first_pts=0[3a];
[1a][2a][3a]amix=inputs=3[a]" \
-map 0:v -map [a] \
-c:v copy -c:a aac \
./temp/withoutsubs.mp4
Related
Text overlay does not work if framenum 1..n and timecode hh:mm:ss:frame attributes are used together, how do overlay both running values?
Does not work, only timecode is incrementing and framenum is a as a text:
ffmpeg.exe -hide_banner -nostats ^
-i "video_25fps.mp4" -threads 4 -preset fast ^
-c:v libx264 -profile:v main -level 4.0 -s:v 640x360 -b:v 512k -pix_fmt yuv420p ^
-refs 3 -bf 3 -g 50 -keyint_min 25 -b_strategy 1 -flags +cgop -sc_threshold 0 ^
-movflags "negative_cts_offsets+faststart" ^
-vf "drawtext=fontfile=/fonts/Roboto-Regular.ttf:fontcolor=White:fontsize=38:box=1:boxcolor=black:x=(w-text_w)/2:y=text_h-line_h+60:text='H264\ 640x360\ 512k\ frame\: %{frame_num}\ ':start_number=1:timecode=00\\:00\\:00\\:00:rate=25" ^
-an -sn -t 30 -y output.mp4
Works if framenum only:
-vf "drawtext=fontfile=/fonts/Roboto-Regular.ttf:fontcolor=White:fontsize=38:box=1:boxcolor=black:x=(w-text_w)/2:y=text_h-line_h+60:text='H264\ 640x360\ 512k\ frame\: %{frame_num}':start_number=1"
Works if timecode only:
-vf "drawtext=fontfile=/fonts/Roboto-Regular.ttf:fontcolor=White:fontsize=38:box=1:boxcolor=black:x=(w-text_w)/2:y=text_h-line_h+60:text='H264\ 640x360\ 512k\ ':timecode='00\:00\:00\:00':rate=25"
You need to double up on the drawtext instruction, separating them with a comma.
i.e.
ffmpeg -i input.mp4 -vf "drawtext=text='Frame\: %{frame_num}': start_number=1: x=(w-tw)/2: y=h-(2*lh):font='Noto mono':fontsize=40:alpha=0.5:box=1:boxborderw=4,drawtext=text='':x=(w-tw)/2:y=(lh):fontsize=40:fontcolor=white:timecode='00\:00\:00\:00':timecode_rate=25" -c:a copy output2.mp4
Is there a way to change ffmpeg input while streaming to rtmp?
I have this bash script
#! /bin/bash
VBR="1500k"
FPS="24"
QUAL="superfast"
RTMP_URL="rtmp://live.live/live"
KEY="xxxxxxxxxxxxxxxxxxxxx"
VIDEO_SOURCE="video.mp4"
AUDIO_SOURCE="song.mp3"
NP_SOURCE="song.txt"
FONT="font.ttf"
ffmpeg \
-re -f lavfi -i "movie=filename=$VIDEO_SOURCE:loop=0, setpts=N/(FRAME_RATE*TB)" \
-thread_queue_size 512 -i "$AUDIO_SOURCE" \
-map 0:v:0 -map 1:a:0 \
-map_metadata:g 1:g \
-vf drawtext="fontsize=25: fontfile=$FONT: \
box=1: boxcolor=black#0.5: boxborderw=20: \
textfile=$NP_SOURCE: reload=1: fontcolor=white#0.8: x=50: y=50" \
-vcodec libx264 -pix_fmt yuv420p -preset $QUAL -r $FPS -g $(($FPS * 2)) -b:v $VBR \
-acodec libmp3lame -ar 44100 -threads 6 -qscale:v 3 -b:a 320000 -bufsize 512k \
-f flv "$RTMP_URL/$KEY"
What i want to do is to be able to change VIDEO_SOURCE on the fly, i was thinking if it's possible to make the input a directory then change the video in that directory on the fly, i'm new to dealing with scripts so i don't know how to do that
This is a complete guess, based on what little I know about how ffmpeg handles interactive input:
while :; do
ffmpeg \
-re -f lavfi -i "movie=filename=$VIDEO_SOURCE:loop=0, setpts=N/(FRAME_RATE*TB)" \
-thread_queue_size 512 -i "$AUDIO_SOURCE" \
-map 0:v:0 -map 1:a:0 \
-map_metadata:g 1:g \
-vf drawtext="fontsize=25: fontfile=$FONT: \
box=1: boxcolor=black#0.5: boxborderw=20: \
textfile=$NP_SOURCE: reload=1: fontcolor=white#0.8: x=50: y=50" \
-vcodec libx264 -pix_fmt yuv420p -preset $QUAL -r $FPS -g $(($FPS * 2)) -b:v $VBR \
-acodec libmp3lame -ar 44100 -threads 6 -qscale:v 3 -b:a 320000 -bufsize 512k \
-f flv "$RTMP_URL/$KEY"
read -p "Next movie?" VIDEO_SOURCE
[ "$VIDEO_SOURCE" = q ] && break
done
ffmpeg should(?) exit if you send q to standard input. Your script will then prompt you for a new value for VIDEO_SOURCE. If you type q again, the loop exits. Otherwise, it restarts ffmpeg with the new video source file.
If this works, you can perhaps adapt it for something closer to your needs.
I am looking to encode a file and generate multiple periods rather than just one period. When I try to run ffmpeg with the below command, the result is a single period with multiple adaptation sets so clearly that is the wrong approach. I had thought by breaking the files by time this might work but apparently not. Any advice would be appreciated:
ffmpeg -i ~/Downloads/turkish-horses.mp4 \
-map 0:v:0 -b:v:1 1000k -ss 0 -t 5 -c:v:1 libx264 -filter:v:1 "scale=640:-1" \
-map 0:v:0 -b:v:2 1000k -ss 5 -c:v:2 libx264 -filter:v:2 "scale=640:-1" \
-map 0:v:0 -b:v:3 3000k -ss 0 -t 5 -c:v:3 libx264 -filter:v:3 "scale=1280:-1" \
-map 0:v:0 -b:v:4 3000k -ss 5 -c:v:4 libx264 -filter:v:4 "scale=1280:-1" \
-map 0:a\?:0 -ss 0 -t 5 -c:a aac -b:a 192k \
-map 0:a\?:0 -ss 5 -c:a aac -b:a 192k \
-use_timeline 1 -use_template 0 \
-adaptation_sets "id=0,streams=0,2 id=1,streams=4 id=2,streams=1,3 id=3,streams=5" \
-f dash output.mpd
I wanted to record multiple screens simultaneously on MacOS and store them in a video file.Same as the screenshot below.
I found a command to do this, but need to know the size of each screen.
ffmpeg \
-f avfoundation -pix_fmt uyvy422 -i 1 \
-f avfoundation -pix_fmt uyvy422 -i 2 \
-pix_fmt yuv420p -r 30 -preset ultrafast -b:v 5000k -t 15 \
-filter_complex \
"nullsrc=size=3286x1080 [background]; \
[0:v] setpts=PTS-STARTPTS [left]; \
[1:v] setpts=PTS-STARTPTS [right]; \
[background][left] overlay=shortest=1 [background+left]; \
[background+left][right] overlay=shortest=1:x=1366 [left+right]" \
-map [left+right] out.mp4 -y
Is there a way to record a screen without knowing the size of each screen?
You can do it using the new xstack filter.
Use
ffmpeg \
-f avfoundation -pix_fmt uyvy422 -i 1 \
-f avfoundation -pix_fmt uyvy422 -i 2 \
-filter_complex \
[0:v] setpts=PTS-STARTPTS [left]; \
[1:v] setpts=PTS-STARTPTS [right]; \
[left][right] xstack=inputs=2:layout=0_0|w0_0 [left+right]" \
-map [left+right] -pix_fmt yuv420p -r 30 -preset ultrafast -b:v 5000k -t 15 \ out.mp4 -y
Get the latest dated build from https://ffmpeg.zeranoe.com/builds/macos64/static/
I have a web site in which users can upload video files. I want to stream all of them using DASH to obtain an adaptive bitrate streaming. So I wrote a bash script (to run by cron) that converts all mp4 files to DASH, but it doesn't work properly: what is wrong?
For example, using the following script, I obtained:
https://www.informatica-libera.net/dash_faq/stream.mpd
It validates, but it doesn't play. I tested it on:
http://dash-mse-test.appspot.com/dash-player.html?url=https%3A%2F%2Fwww.informatica-libera.net%2Fdash_faq%2Fstream.mpd&autoplay=on&adapt=auto&flavor=
Thank you for any help.
The code:
#!/bin/bash
# THIS SCRIPT CONVERTS EVERY MP4 (IN THE CURRENT FOLDER AND SUBFOLDER)
# TO A MULTI-BITRATE VIDEO IN MP4-DASH
# For each file "videoname.mp4" it creates a folder "dash_videoname"
# containing a dash manifest file "stream.mpd" and subfolders containing
# video segments.
# mp4dash documentation and download: https://www.bento4.com/developers/dash/
MYDIR=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
SAVEDIR=$(pwd)
# Check programs
if [ -z "$(which ffmpeg)" ]; then
echo "Error: ffmpeg is not installed"
exit 1
fi
if [ -z "$(which mp4dash)" ]; then
echo "Error: mp4dash is not installed"
exit 1
fi
cd "$MYDIR"
TARGET_FILES=$(find ./ -type f -name "*.mp4")
for f in $TARGET_FILES
do
f=$(basename "$f") # fullname of the file
f="${f%.*}" # name without extension
if [ ! -d "dash_${f}" ]; then
echo "Converting \"$f\" to multi-bitrate video in MPEG-DASH"
ffmpeg -y -i "${f}.mp4" -c:a libfdk_aac -ac 2 -ab 128k -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 1500k -vf "scale=-2:720" -f mp4 -pass 1 -y /dev/null
ffmpeg -y -i "${f}.mp4" -c:a libfdk_aac -ac 2 -ab 128k -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 1500k -vf "scale=-2:720" -f mp4 -pass 2 "${f}_1500.mp4"
ffmpeg -y -i "${f}.mp4" -c:a libfdk_aac -ac 2 -ab 128k -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 800k -vf "scale=-2:540" -f mp4 -pass 1 -y /dev/null
ffmpeg -y -i "${f}.mp4" -c:a libfdk_aac -ac 2 -ab 128k -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 800k -vf "scale=-2:540" -f mp4 -pass 2 "${f}_800.mp4"
ffmpeg -y -i "${f}.mp4" -c:a libfdk_aac -ac 2 -ab 128k -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 400k -vf "scale=-2:360" -f mp4 -pass 1 -y /dev/null
ffmpeg -y -i "${f}.mp4" -c:a libfdk_aac -ac 2 -ab 128k -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 400k -vf "scale=-2:360" -f mp4 -pass 2 "${f}_400.mp4"
ffmpeg -y -i "${f}.mp4" -c:a libfdk_aac -ac 2 -ab 128k -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 200k -vf "scale=-2:180" -f mp4 -pass 1 -y /dev/null
ffmpeg -y -i "${f}.mp4" -c:a libfdk_aac -ac 2 -ab 128k -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 200k -vf "scale=-2:180" -f mp4 -pass 2 "${f}_200.mp4"
rm -f ffmpeg*log*
mp4fragment "${f}_1500.mp4" "${f}_1500_fragmented.mp4"
mp4fragment "${f}_800.mp4" "${f}_800_fragmented.mp4"
mp4fragment "${f}_400.mp4" "${f}_400_fragmented.mp4"
mp4fragment "${f}_200.mp4" "${f}_200_fragmented.mp4"
rm -f "${f}_1500.mp4" "${f}_800.mp4" "${f}_400.mp4" "${f}_200.mp4"
mp4dash -v -o "dash_${f}" "${f}_1500_fragmented.mp4" "${f}_800_fragmented.mp4" "${f}_400_fragmented.mp4" "${f}_200_fragmented.mp4"
rm -f "${f}_1500_fragmented.mp4" "${f}_800_fragmented.mp4" "${f}_400_fragmented.mp4" "${f}_200_fragmented.mp4"
fi
done
cd "$SAVEDIR"
I solved so (however it's not fully compatible with every modern browser, for example on my Linux version of Firefox the audio doesn't play):
#!/bin/bash
# THIS SCRIPT CONVERTS EVERY MP4 (IN THE CURRENT FOLDER AND SUBFOLDER) TO A MULTI-BITRATE VIDEO IN MP4-DASH
# For each file "videoname.mp4" it creates a folder "dash_videoname" containing a dash manifest file "stream.mpd" and subfolders containing video segments.
# Validation tool:
# http://dashif.org/conformance.html
# Documentation:
# https://tdngan.wordpress.com/2016/11/17/how-to-encode-multi-bitrate-videos-in-mpeg-dash-for-mse-based-media-players/
# Remember to add the following mime-types (uncommented) to .htaccess:
# AddType video/mp4 m4s
# AddType application/dash+xml mpd
# DASH-264 JavaScript Reference Client
# https://github.com/Dash-Industry-Forum/dash.js
# https://github.com/Dash-Industry-Forum/dash.js/wiki
MYDIR=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
SAVEDIR=$(pwd)
# Check programs
if [ -z "$(which ffmpeg)" ]; then
echo "Error: ffmpeg is not installed"
exit 1
fi
if [ -z "$(which MP4Box)" ]; then
echo "Error: MP4Box is not installed"
exit 1
fi
cd "$MYDIR"
TARGET_FILES=$(find ./ -type f -name "*.mp4")
for f in $TARGET_FILES
do
f=$(basename "$f") # fullname of the file
f="${f%.*}" # name without extension
if [ ! -d "dash_${f}" ]; then
echo "Converting \"$f\" to multi-bitrate video in MPEG-DASH"
ffmpeg -y -i "${f}.mp4" -c:a libfdk_aac -ac 2 -ab 128k -vn "${f}_audio.m4a"
ffmpeg -y -i "${f}.mp4" -an -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 1500k -vf "scale=-2:720" -f mp4 -pass 1 -y /dev/null
ffmpeg -y -i "${f}.mp4" -an -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 1500k -vf "scale=-2:720" -f mp4 -pass 2 "${f}_1500.mp4"
ffmpeg -y -i "${f}.mp4" -an -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 800k -vf "scale=-2:540" -f mp4 -pass 1 -y /dev/null
ffmpeg -y -i "${f}.mp4" -an -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 800k -vf "scale=-2:540" -f mp4 -pass 2 "${f}_800.mp4"
ffmpeg -y -i "${f}.mp4" -an -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 400k -vf "scale=-2:360" -f mp4 -pass 1 -y /dev/null
ffmpeg -y -i "${f}.mp4" -an -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 400k -vf "scale=-2:360" -f mp4 -pass 2 "${f}_400.mp4"
ffmpeg -y -i "${f}.mp4" -an -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 200k -vf "scale=-2:180" -f mp4 -pass 1 -y /dev/null
ffmpeg -y -i "${f}.mp4" -an -c:v libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 200k -vf "scale=-2:180" -f mp4 -pass 2 "${f}_200.mp4"
rm -f ffmpeg*log*
MP4Box -dash 2000 -rap -frag-rap -profile onDemand "${f}_1500.mp4" "${f}_800.mp4" "${f}_400.mp4" "${f}_200.mp4" "${f}_audio.m4a" -out "${f}_MP4.mpd"
rm "${f}_1500.mp4" "${f}_800.mp4" "${f}_400.mp4" "${f}_200.mp4" "${f}_audio.m4a"
fi
done
cd "$SAVEDIR"
I also tried VP9 instead of h.264, but also in this case there isn't compatibility with all browser (in my Linux distro, it plays correctly only on Firefox, while it doesn't play at all on Chrome):
#!/bin/bash
MYDIR=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
SAVEDIR=$(pwd)
# Controlla che i programmi richiesti siano installati
if [ -z "$(which ffmpeg)" ]; then
echo "Errore: ffmpeg non e' installato"
exit 1
fi
cd "$MYDIR"
TARGET_FILES=$(find ./ -type f -name "*.mp4")
for f in $TARGET_FILES
do
f=$(basename "$f") # memorizza il nome completo del file
f="${f%.*}" # toglie l'estensione
if [ ! -f "${f}.mpd" ]; then
echo "Converto il file \"$f\" in Adaptive WebM using DASH"
echo "Riferimenti: http://wiki.webmproject.org/adaptive-streaming/instructions-to-playback-adaptive-webm-using-dash"
# http://wiki.webmproject.org/adaptive-streaming/instructions-to-playback-adaptive-webm-using-dash
VP9_DASH_PARAMS="-tile-columns 4 -frame-parallel 1"
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:90 -b:v 250k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:90 -b:v 250k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_160px_250k.webm"
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:180 -b:v 500k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:180 -b:v 500k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_320px_500k.webm"
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:360 -b:v 750k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:360 -b:v 750k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_640px_750k.webm"
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:360 -b:v 1000k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:360 -b:v 1000k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_640px_1000k.webm"
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:720 -b:v 1500k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:720 -b:v 1500k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_1280px_1500k.webm"
ffmpeg -i "${f}.mp4" -c:a libvorbis -b:a 128k -vn -f webm -dash 1 "${f}_audio_128k.webm"
rm -f ffmpeg*.log
ffmpeg \
-f webm_dash_manifest -i "${f}_160px_250k.webm" \
-f webm_dash_manifest -i "${f}_320px_500k.webm" \
-f webm_dash_manifest -i "${f}_640px_750k.webm" \
-f webm_dash_manifest -i "${f}_640px_1000k.webm" \
-f webm_dash_manifest -i "${f}_1280px_1500k.webm" \
-f webm_dash_manifest -i "${f}_audio_128k.webm" \
-c copy -map 0 -map 1 -map 2 -map 3 -map 4 -map 5 \
-f webm_dash_manifest \
-adaptation_sets "id=0,streams=0,1,2,3,4 id=1,streams=5" \
"${f}.mpd"
fi
done
cd "$SAVEDIR"
I didn't find a way to serve audio/video content to all browsers. I've done my tests so:
<!DOCTYPE html>
<html>
<head>
<script src="http://cdn.dashjs.org/latest/dash.all.min.js"></script>
<style>
video {
width: 640px;
height: 360px;
}
</style>
</head>
<body>
<div>
<video data-dashjs-player autoplay src="test.mpd" controls></video>
</div>
</body>
</html>