[FFmpeg-user] Splitting a video in chunks with the same size

blubee blubeeme gurenchan at gmail.com
Wed Jun 8 16:40:18 CEST 2016


Here is the script
In this script chunk_size and default_chunk_size = 600 seconds so my chunks
were 10 minutes, change the chunk size to whatever you like.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
DIR="path/to/your/videos/*" #notice the trailing /*
OUT_DIR="path/where/output/files/go"
OUTPUT_FILE=""

file_duration_float=0
file_duration_int=0
working_file_duration=0
default_chunk_size=600 #video length in seconds (10 minutes)
chunk_size=600 #video length in seconds (10 minutes)
chunk_start_time=0
chunk_end_time=0
iteration=0

#extract simple metadata from files
for f in $DIR
do
echo $f
file_duration_float=$(ffprobe -i "$f" -show_format -v quiet | sed -n
's/duration=//p')
file_duration_int=$( echo "($file_duration_float+0.5)/1" | bc )
working_file_duration_int=$file_duration_int
echo $file_duration_int

#---------------------------------------------
# determine the start and end time of each clip
#---------------------------------------------
while [ $working_file_duration_int -gt -1 ]
do
filename=$(basename "$f")
input_file="$filename"
filename=${filename::-4}" "
file_extension=${f: -3}
input_file_dir=$(dirname "$f")"/"


if [ $iteration -eq 0 ]
then
            if [ $file_duration_int -le $chunk_size ]
            then
   working_file_duration_int=$((file_duration_int))
   chunk_start_time=0
   chunk_end_time=$(($working_file_duration_int))
                echo "file is shorter than 10 minutes:
$working_file_duration_int  breaking early! iteration $iteration start:
$chunk_start_time end: $working_file_duration_int duration:
$working_file_duration_int video length left $working_file_duration_int"

OUTPUT_FILE="$OUT_DIR$filename$iteration.mkv"
$(ffmpeg -hide_banner -loglevel panic -i "$f" -ss $chunk_start_time -t
$chunk_end_time [your options here ] "$OUTPUT_FILE")
                break
            fi

working_file_duration_int=$((file_duration_int - chunk_size))
chunk_start_time=0
chunk_end_time=$(($default_chunk_size))

echo "iteration $iteration start: $chunk_start_time end: $chunk_end_time
duration: $default_chunk_size video length left $working_file_duration_int"

OUTPUT_FILE="$OUT_DIR$filename$iteration.mkv"
$(ffmpeg -hide_banner -loglevel panic -i "$f" -ss $chunk_start_time -t
$chunk_end_time [your options here ] "$OUTPUT_FILE")
iteration=$((iteration+1))
fi
if [ $working_file_duration_int -le $chunk_size ]
then
chunk_size=$working_file_duration_int
working_file_duration_int=$((working_file_duration_int - chunk_size))
chunk_start_time=$(($chunk_start_time+$chunk_end_time))
chunk_end_time=$(($chunk_size))

echo "early chunk size: $chunk_size iteration $iteration start:
$chunk_start_time end: $((chunk_start_time+chunk_end_time)) duration:
$chunk_end_time video length left $working_file_duration_int "

OUTPUT_FILE="$OUT_DIR$filename$iteration.mkv"
$(ffmpeg -hide_banner -loglevel panic -i "$f" -ss $chunk_start_time -t
$chunk_end_time [your options here ] "$OUTPUT_FILE")
iteration=0
chunk_size=$default_chunk_size
break
else
chunk_start_time=$(($chunk_start_time+$chunk_end_time))
chunk_end_time=$(($chunk_size))
working_file_duration_int=$((working_file_duration_int - chunk_size))
echo "iteration $iteration start: $chunk_start_time end:
$((chunk_start_time+chunk_end_time)) duration: $chunk_end_time video length
left $working_file_duration_int "

OUTPUT_FILE="$OUT_DIR$filename$iteration.mkv"
$(ffmpeg -hide_banner -loglevel panic -i "$f" -ss $chunk_start_time -t
$chunk_end_time [your options here ] "$OUTPUT_FILE")
iteration=$((iteration+1))
fi
done
echo "---------------- finished processing $f"
done
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Let me know if you have any questions.

On Wed, Jun 8, 2016 at 5:14 PM, Henk D. Schoneveld <belcampo at zonnet.nl>
wrote:

>
> On 08 Jun 2016, at 11:01, blubee blubeeme <gurenchan at gmail.com> wrote:
>
> > I wrote a script to do this exact same thing. I will post it to this list
> > when I get back to my machine.
> It can be done by splitting an all i-frame/key-frame mpeg transport stream
> file with the split command. An all i-frame/key-frame conversion isn’t very
> efficient in conversion, but that is a side-effect.
> >
> > On Wed, Jun 8, 2016, 16:56 Henk D. Schoneveld <belcampo at zonnet.nl>
> wrote:
> >
> >>
> >> On 08 Jun 2016, at 10:39, Celso Coutinho <20.celso at gmail.com> wrote:
> >>
> >>> Dear all,
> >>>
> >>> I want to be able to split a file in chunks with the same (previously
> >>> defined) size (except the last chunk, of course). I have been trying
> >>> several commands but without much success.
> >> Out of curiosity, what is the reason for wanting same-sized chunks ?
> >> Same-time chunks is what mostly is used.
> >>>
> >>> When I look into the documentation, I understand the *-fs* flag allows
> me
> >>> to define the maximum size of the chunk I am obtaining in each time I
> run
> >>> *ffmpeg,* but I don't know how to define the starting point of the
> >> chunk. I
> >>> tried *-ss*, but it seems that I can only use it with time, rather than
> >>> file size. Can someone enlighten me on this matter?
> >>>
> >>> Moreover, I have been trying to use the following command:
> >>>
> >>> *ffmpeg **-i raper.3gp -fs 102400 -vcodec copy -acodec copy -movflags
> >>> faststart output1.avi*
> >>>
> >>> But the output video has no sound, plus the duration is always
> incorrect
> >>> (video has 4 to 5 seconds, and the duration shown in the video is like
> >> one
> >>> minute). Which codecs should I choose?
> >>>
> >>> Thanks in advance!
> >>>
> >>> Celso
> >>> _______________________________________________
> >>> ffmpeg-user mailing list
> >>> ffmpeg-user at ffmpeg.org
> >>> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
> >>>
> >>> To unsubscribe, visit link above, or email
> >>> ffmpeg-user-request at ffmpeg.org with subject "unsubscribe".
> >>
> >> _______________________________________________
> >> ffmpeg-user mailing list
> >> ffmpeg-user at ffmpeg.org
> >> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
> >>
> >> To unsubscribe, visit link above, or email
> >> ffmpeg-user-request at ffmpeg.org with subject "unsubscribe".
> > _______________________________________________
> > ffmpeg-user mailing list
> > ffmpeg-user at ffmpeg.org
> > http://ffmpeg.org/mailman/listinfo/ffmpeg-user
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-user-request at ffmpeg.org with subject "unsubscribe".
>
> _______________________________________________
> ffmpeg-user mailing list
> ffmpeg-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
>
> To unsubscribe, visit link above, or email
> ffmpeg-user-request at ffmpeg.org with subject "unsubscribe".
>


More information about the ffmpeg-user mailing list