Dividing, processing and merging files with ffmpeg - merge

I am trying to build an application that will divide an input video file (usually mp4) into chunks so that I can apply some processing to them concurrently and then merge them back into a single file.
To do this, I have outlined 4 steps:
Forcing keyframes at specific intervals so to make sure that each
chunk can be played on its own. For this I am using the following
command:
ffmpeg -i input.mp4 -force_key_frames
"expr:gte(t,n_forced*chunk_length)" keyframed.mp4
where chunk_length is the duration of each chunk.
Dividing keyframed.mp4 into multiple chunks.
Here is where I have my problem. I am using the following command:
`ffmpeg -i keyframed.mp4 -ss 00:00:00 -t chunk_length -vcodec copy -acodec copy test1.mp4`
to get the first chunk from my keyframed file but it isn't capturing
the output correctly, since it appears to miss the first keyframe.
On other chunks, the duration of the output is also sometimes
slightly less than chunk_length, even though I am always using the
same -t chunk_length option
Processing each chunk For this task, I am using the following
commands:
ffmpeg -y -i INPUT_FILE -threads 1 -pass 1 -s 1280x720 -preset
medium -vprofile baseline -c:v libx264 -level 3.0 -vf
"format=yuv420p" -b:v 2000k -maxrate:v 2688k -bufsize:v 2688k -r 25
-g 25 -keyint_min 50 -x264opts "keyint=50:min-keyint=50:no-scenecut" -an -f mp4 -movflags faststart /dev/null
ffmpeg -y -i INPUT_FILE -threads 1 -pass 2 -s 1280x720 -preset
medium -vprofile baseline -c:v libx264 -level 3.0 -vf
"format=yuv420p" -b:v 2000k -maxrate:v 2688k -bufsize:v 2688k -r 25
-g 25 -keyint_min 50 -x264opts "keyint=50:min-keyint=50:no-scenecut" -acodec libfaac -ac 2 -ar 48000 -ab 128k -f mp4 -movflags faststart OUTPUT_FILE.mp4
This commands are not allowed to be modified, since my goal here is
to parallelize this process.
Finally, to merge the files I am using concat and a list of the
outputs of the 2nd step, as follows:
ffmpeg -f concat -i mylist.txt -c copy final.mp4
In conclusion, I am trying to find out a way to solve the problem with step 2 and also get some opinions if there is a better way to do this.

I found a solution with the following code, which segments the file without the need to force keyframes (it cuts on the nearest keyframe) and multiple commands.
ffmpeg -i test.mp4 -f segment -segment_time chunk_length -reset_timestamps 1 -c copy test%02d.mp4

Related

Raspberry Pi libcamera-vid to Youtube

I'm setting up a nature cam using a Raspberry Pi 4 livestreaming to Youtube. I can live stream video to Youtube using:
raspivid -o - -t 0 -w 1280 -h 720 -fps 25 -b 4000000 -g 50 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f vs16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/<mykey>
but this requires legacy support to be enabled - which means I can't remote to my pi using VNC. I can use Putty to run the raspivid command, but I then need to have another computer running Youtube in a browser to enable the live stream. I'd rather just have the Pi do this, but I can't open Chromium from the Putty command line. If I turn off legacy support, I can use VNC and run Chromium, but I can't run Raspivid. libcamera-vid is meant to replace Raspivid, but I have not found anything that tells me what settings to use.
libcamera-vid -o - -t 0 --width 854 --height 480 --brightness 0.1 --inline --autofocus --framerate 25 -g 50 | ffmpeg -f lavfi -i anullsrc -thread_queue_size 1024 -use_wallclock_as_timestamps 1 -i pipe:0 -c:v copy -b:v 2500k -f flv rtmp://a.rtmp.youtube.com/live2/mykey
gives errors, particularly around audio settings (my Pi isn't recording audio).
I'd be grateful if someone could give me a newbies guide to converting Raspivid commands to Libcamera-vid!
Thanks
Thanks
Yes, you have to define null audio like this -i anullsrc=channel_layout=stereo:sample_rate=44100
So I have something similar to you:
libcamera-vid --inline --nopreview -t 0 --width 640 --height 480 --framerate 15 --codec h264 -o - | ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -thread_queue_size 1024 -use_wallclock_as_timestamps 1 -i pipe:0 -c:v copy -c:a aac -preset fast -strict experimental -f flv rtmp://0.0.0.0:1935/live/1

Better way to use ffmpeg with vidstab and encoding 2 pass

I scan old 8mm films
so I have folder with set of jpeg
I transform them to films using ffmpeg ( I choose x264 2 pass encoding)
//On all folder that start by 1 I launch the pass1 for x264
for f in 1*/ ; do cd "$f"; ffmpeg -y -r 18 -i img%05d.jpg -c:v libx264 -s 1200x898 -b:v 3000k -pass 1 -an -f mp4 /dev/null; cd ..; done
//On all folder that start by 1 I launch the pass2 x264
for f in 1*/ ; do cd "$f"; ffmpeg -y -r 18 -i img%05d.jpg -c:v libx264 -s 1200x898 -b:v 3000k -pass 2 ../"`echo ${PWD##*/}`.mp4"; cd ..; done
--> Before I have set of folder with jpeg
1965-FamilyStuff01\img1111.jpg,..,img9999.jpg
1965-FamilyStuff02\img1111.jpg,..,img9999.jpg
and I get
1965-FamilyStuff01.mp4
1965-FamilyStuff02.mp4
then I discover vidstab that also need 2 pass
// Stabilize every Video of a folder
mkdir stab;for f in ./*.mp4 ; do echo "Stabilize $f" ;
ffmpeg -i "$f" -vf vidstabdetect=shakiness=5:accuracy=15:stepsize=6:mincontrast=0.3:show=2 -y -f mp4 /dev/null;
ffmpeg -i "$f" -vf vidstabtransform=smoothing=30:input="transforms.trf":interpol=linear:crop=black:zoom=0:optzoom=1,unsharp=5:5:0.8:3:3:0.4 -y "stab/$f"
; done; rm transforms.trf;
But I ask myself, that perhaps the order is not correct or perhaps there is a way to do the encoding with vidstab in less than 4 pass (2 pass for x264 encoding then 2 pass for vidstab)
or perhaps the order should be change to optimize quality of film output)
You will need to run two commands to use vidstab. But x264 does not need two-passes for best quality. Two-pass encoding is used to target a specific output file size. Just use a single pass with the -crf option.
So you only need to use two commands:
ffmpeg -i input.mp4 -vf "scale=1200:-2,vidstabdetect=shakiness=5:accuracy=15:stepsize=6:mincontrast=0.3:show=2" -f null -
ffmpeg -i input.mp4 -vf "scale=1200:-2,vidstabtransform=smoothing=30:interpol=linear:crop=black:zoom=0:optzoom=1,unsharp=5:5:0.8:3:3:0.4,format=yuv420p" -crf 23 -preset medium output.mp4
See FFmpeg Wiki: H.264 for more info on -crf and -preset.

Text streaming with RTMP?

I'm trying to get the output of a bash file to an RTMP stream.
I've successfully done it with FFMPEG using a filter, but the stream stops at Random intervals.
I assume that it's FFMPEG reading NULL data from the file.
I already write another file "output.txt", delete " input.txt" (which FFMPEG is reading) and rename "output.txt" to "input.txt".
Is there any way to do it more atomic in bash so it will work? Or is there a more elegant way to turn a changing text (max one time per second) to an FFMPEG stream?
Here is my current script:
ffmpeg -s 1920x1080 -f rawvideo -pix_fmt rgb24 -r 10 -i /dev/zero -f lavfi -i anullsrc -vcodec h264 -pix_fmt yuv420p -r 10 -b:v 2500k -qscale:v 3 -b:a 712000 -bufsize 512k -vf "drawtext=fontcolor=0xFFFFFF:fontsize=15:fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf:textfile=input.txt:x=0:y=0:reload=1" -f flv "rtmp://example.com/key"

FFmpeg - Down-mix AC3 5.1 to Fraunhofer FDK ACC 2.1

I'm trying to re-encode some of my old videos to "archive" them.
I do not need to keep the audio 5.1, but I would like to down-mix it to 2.1 instead of Stereo which sounds just too dull.
This is the relevant part which takes care of the down-mix to Stereo and re-encodes the audio, I would like to adjust it to down-mix to 2.1.
-ac 2 -c:a libfdk_aac -vbr 3
I did some research and it seems that there is a -layouts switch which does support 2.1, but I don't know, how to use it. What channel should go where?
Just for illustration and for you to get the whole picture - I'm currently using this script:
#!/bin/bash
for i in *.mkv;
do
#Output new files by prepending "x265" to the names
/cygdrive/c/media-autobuild_suite/local32/bin-video/ffmpeg.exe -y -i "$i" -c:v libx265 -preset slow -b:v 512k -x265-params pass=1 -c:s copy -c:a copy -f matroska NUL && \
/cygdrive/c/media-autobuild_suite/local32/bin-video/ffmpeg.exe -i "$i" -c:v libx265 -preset slow -b:v 512k -x265-params pass=2 -c:s copy -ac 2 -c:a libfdk_aac -vbr 3 x265_"$i"
done
The FDK aac encoder does not support 2.1, but the native encoder does.
ffmpeg -i "$i" ... -c:s copy -af pan=2.1 -c:a aac x265_"$i"

FFmpeg - The two pass in VP9 generates a empty output file for the first pass

When I try to encode a video file with a two pass in ffmpeg, the output file of the first pass is empty using vp9. Hence I can not proceed with the second part.
Code for the two-pass:
1.pass:
ffmpeg -y -s:v 3840x1920 -framerate 30 -i video_framerate_resolution.yuv -c:v libvpx-vp9 -crf 20
-pass 1 -an -f avi NULL && \
2.pass
ffmpeg -s:v 3840x1920 -framerate 30 -i video_framerate_resolution.yuv -c:v libvpx-vp9
-pass 2 -b:v 1000K -f avi out.avi
Any help would be greatly appreciated. Thanks.
You don't need to generate a file for the first pass. The purpose is simply to send the frames to the encoder so that it can log stats. However, you should skip the muxer.
So, Pass 1
ffmpeg -s:v 3840x1920 -framerate 30 -i video_framerate_resolution.yuv -c:v libvpx-vp9 -b:v 1000k -pass 1 -an -f null -
Pass 2
ffmpeg -s:v 3840x1920 -framerate 30 -i video_framerate_resolution.yuv -c:v libvpx-vp9 -pass 2 -b:v 1000K out.avi