Video Compression

VideoNerd

Shortly on MPEG-DASH

DASH is a complex hierarchical schema:  MPD-file (the manifest file) is divided into periods, each period is divided into adaptation sets, adaptation set is divided in representations and each representation is divided into segments:

 

 

 

 

How Generate MPEG-DASH with shaka-packager

A single DASH segment is commonly not decodable since SPS/PPS are not present in the meta-data of segments. They are present in a special ‘init’ mp4-file. Even if you extract the video stream from such mp4 segment it would not playable, you need add SPS/PPS.

To extract the video stream form a mp4 segment i suggest to perform the following operations:

1)  to concatenate ‘init’ mp4 file with the segment file (e.g. by using Linux command ‘cat’)

2) then to apply ffmpeg to extract video stream with SPS/PPS present.  For example:

       ffmpeg -i test.mp4 -vcodec copy -vbsf h264_mp4toannexb -f h264 test.h264

 

Example: How Shaka packager generates MPEG-DASH mp4 segments with the init-file (test_init.mp4):

/packager-linux ‘in=test.ts , stream=video, init_segment=test_init.mp4, segment_template=test_$Number$.mp4’ –profile vod –mpd_output test.mpd –dump_stream_info –min_buffer_time=0.1 –segment_duration=0.5 –strip_parameter_set_nalus=false

As a results test_1.mp4, test_2.mp4, … segments are generated and in addition test_init.mp4 file is also produced (this file test_init.mp4 contains SPS/PPS).

 

How Assembly All Segments

Shaka Packager produces tons of mp4-segments and it can be required to extract video excerpts from all mp4-segments and to assemble the overall elementary video stream. The following bash-script   ExtractVideoSegments.sh extracts all video excerpts and compose them in correct order (consequently the overall elementary video stream is generated):

#!/bin/bash

if (( $#<2 ))then

    echo ‘Usage: indir  init-mp4  outstream  tempfolder (default  /.)’ >&2

   exit 1

fi

 

indir=$1

initmp4=$2

outfile=$3

tempdir=${4:-./}

if [ ! -d $indir ]; then

    echo $indir  is not directory or not exist

    exit 1

fi

if [ ! -f $initmp4 ]; then

    echo $initmp4  is not exist

    exit 1

fi

if [ ! -d $tempdir ]; then

     echo $tempdir  is not directory or not exist

    exit 1

fi

if [  -d $outfile ]; then

    echo $outfile  is directory

    exit 1

fi

if [ -f $outfile ]; then   

    echo $outfile exist and to be overwritten   

   rm   $outfile

fi

# add ‘/’ to the end folder path if it’s absent

lastSym=”${indir: -1}”

if [[ $lastSym != “/” ]]; then   

     indir=”${indir}/”

fi

lastSym=”${tempdir: -1}”

if [[ $lastSym != “/” ]]; then  

    tempdir=”${tempdir}/”

fi

 nfiles=0

for mp4file in $( ls ${indir}*.mp4 );

do   

    bname=$(basename “$mp4file”)    # filename without directory path

    fname=”${bname%.*}”                  # filename without extension

   lastSym=”${fname: -1}”    # files with names not ending numerals are not segments, even if they are segments we can’t sorted 

   if [[ $lastSym == [0-9] ]]; then 

       mp4segment=”${tempdir}${fname}.mp4″

        h264segment=”${tempdir}${fname}.264″

        cat $initmp4  $mp4file > $mp4segment 

       ffmpeg  -loglevel quiet  -i $mp4segment -vcodec copy -vbsf h264_mp4toannexb -f h264 -y $h264segment 

     # get file pattern

     if (( nfiles==0 ))   

     then

            filepattern=${fname%_*}          

           echo “file pattern  $filepattern” 

     fi   

    rm $mp4segment  

   let nfiles++   

fi

    done

    echo

    echo “total number of segments $nfiles”

    echo

    cnt=1

    touch $outfile   # create empty file

    while [ $cnt -le $nfiles ]   

   do

         filename=”${tempdir}${filepattern}_${cnt}.264″    

        if [ ! -f $filename ]; then

            echo $filename   not exist

            exit 2    

        fi  

      cat $filename >> $outfile 

       let cnt++  

  done

  exit 0

 

 

Usage of the above script is simple:

indir – the directory where mp4-segments are located. Notice that each segment should have the extension ‘mp4’ and patterned as ‘xxxx_<num>.mp4’ , e.g ‘slava_13.mp4’

init-mp4   – init-mp4 file

 outstream – output h264 file

tempfolder – folder where intermediate files are stored

 

22 Responses

  1. I am not positive the place you’re getting your information, but great topic. I needs to spend a while studying more or figuring out more. Thank you for wonderful info I was searching for this information for my mission.

  2. Awsome post and straight to the point. I don’t know if this is in fact the best place to ask but do you folks have any thoughts on where to hire some professional writers? Thx 🙂

  3. I like what you guys are up also. Such clever work and reporting! Keep up the excellent works guys I’ve incorporated you guys to my blogroll. I think it will improve the value of my web site 🙂

  4. I will immediately seize your rss as I can’t find your email subscription hyperlink or newsletter service. Do you have any? Kindly permit me know in order that I may subscribe. Thanks.

  5. I’ve been surfing on-line more than three hours lately, but I never discovered any attention-grabbing article like yours. It is lovely worth sufficient for me. In my view, if all web owners and bloggers made good content material as you probably did, the internet might be much more useful than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *