User Tools

Site Tools


howto:convert-images

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
howto:convert-images [2024/02/26 19:18] – [downscale video to 720p mkv] Wulf Rajekhowto:convert-images [2025/01/05 18:28] (current) Wulf Rajek
Line 28: Line 28:
 </code> </code>
  
-===== lossless changing container from webm to mkv =====+===== lossy conversion of png to jpg ===== 
 +Converts all png files in the directory recursively to jpg 
 +<code> 
 +find . -iname '*.png' | while read i; do mogrify -format jpg "$i" && rm "$i"; echo "Converted $i to ${i%.*}.jpg"; done 
 +</code> 
 + 
 +===== lossless changing containers between webm/mov/mp4/mkv =====
  
-Webm is a container similar to mkv and can contain various encoded video or audio streams. To change the container from webm to mkv, the following command can be used:+webm/mov/mkv/mp4 are container formats and can contain various encoded video or audio streams. To change the container between each other, the following command can be used, provided the used codecs inside the containers are compatible:
 <code> <code>
 ffmpeg -i file.webm -c:a copy -c:v copy file.mkv ffmpeg -i file.webm -c:a copy -c:v copy file.mkv
  
 #recursively changing containers: #recursively changing containers:
-find . -name '*.webm' -type f -exec bash -c 'ffmpeg -i "$0" -c:a copy -c:v copy "${0%.webm}.mkv"' {} \; +find . -iname '*.webm' -type f -exec bash -c 'ffmpeg -i "$0" -c:a copy -c:v copy "${0%.*}.mkv"' {} \; 
-#find . -type f -name '*.webm' -delete+#find . -type f -iname '*.webm' -delete 
 + 
 +#lossless change container mkv, webm, mov -> mp4 
 +find . -type f \( -iname "*.mkv" -o -iname "*.webm -o -iname "*.mov\) | while read f; do 
 +    ffmpeg -i "$f" -codec copy "${f%.*}.mp4" 
 +done
 </code> </code>
  
-===== change container mkv <-> mp4 ===== +===== high quality conversion to mp4 =====
- +
-To only change the container use the following example command - provided the codecs are compatible with mp4. The quality and size stays the same:+
 <code> <code>
-ffmpeg -i input.mkv -codec copy output.mp4+#High quality non-mp4 to mp4 conversion 
 +find . -type f \( -iname "*.mpg" -o -iname "*.asf" -o -iname "*.wmv" -o -iname "*.mpg" -o -iname "*.mpeg" -o -iname "*.avi" -o -iname "*.divx" -o -iname "*.rmvb" -o -iname "*.rm" -o -iname "*.m4v" -o -iname "*.flv" \) | while read f; do 
 +    ffmpeg -nostdin -i "$f" -vcodec libx264 -acodec aac "${f%.*}.mp4
 +done
 </code> </code>
 +===== find all non-mp4 files =====
 +<code>
 +#Find non-mp4 files
 +find . -type f -not \( -iname "*.mp4" \)
 +</code>
 +
 +
 +
 +
  
 ===== downscale video to 720p mkv ===== ===== downscale video to 720p mkv =====
Line 93: Line 114:
     if [ -f "$input" ]; then     if [ -f "$input" ]; then
         echo "Processing file: $input"         echo "Processing file: $input"
-        output="${input%.*}.convertednew.$container"+        output="${input%.*}.convertednew"
         # Check if mp4 container is desired and if subtitle is ASS format, then convert to srt, otherwise just copy subs         # Check if mp4 container is desired and if subtitle is ASS format, then convert to srt, otherwise just copy subs
         subtitles_present=$(ffprobe -v error -select_streams s -show_entries stream=codec_name -of csv=p=0:s=x "$input")         subtitles_present=$(ffprobe -v error -select_streams s -show_entries stream=codec_name -of csv=p=0:s=x "$input")
Line 101: Line 122:
         else         else
             subtitle_option="-scodec copy"             subtitle_option="-scodec copy"
 +        fi
 +        #check framerate to only ever reduce it and not increase it
 +        # ffprobe returns 25/1 or 24000/1001. result needs to be calculated and needs to be an integer, thus the bash $(( )).
 +        fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v -show_entries stream=avg_frame_rate "$input" | sed 's#/# / #g')
 +        if [ "$fps" == "0 / 0" ]; then
 +            # some video files return 0 / 0 for avg_frame_rate, using r_frame_rate instead
 +            fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v -show_entries stream=r_frame_rate "$input" | sed 's#/# / #g')
 +        fi
 +        fps=$(( $fps ))
 +        if [ $fps -gt 30 ]; then
 +            fps_option="-r 30"
 +        else
 +            fps_option=""
         fi         fi
         #-c:a copy   #copies audio as is, but mp4 works best with aac and wma cannot be in mp4 files         #-c:a copy   #copies audio as is, but mp4 works best with aac and wma cannot be in mp4 files
         #$binary -i "$input" -vf scale=-2:$resolution -c:v libx264 -r 30 -crf 28 -c:a aac -scodec copy -b:v $bitrate -maxrate $bitrate -bufsize $bufsize "$output"         #$binary -i "$input" -vf scale=-2:$resolution -c:v libx264 -r 30 -crf 28 -c:a aac -scodec copy -b:v $bitrate -maxrate $bitrate -bufsize $bufsize "$output"
         # using complex filter to prevent upscaling and only ever downscale         # using complex filter to prevent upscaling and only ever downscale
-        cmd=$binary' -i "'"$input"'" -filter_complex "scale=ceil(iw*min(1\,min('$maxwidth'/iw\,'$maxheight'/ih))/2)*2:-2" -c:v libx264 -r 30 -crf 28 -c:a aac '$subtitle_option' -b:v '$bitrate' -maxrate '$bitrate' -bufsize '$bufsize' "'"$output"'"' +        cmd=$binary' -i "'"$input"'" -filter_complex "scale=ceil(iw*min(1\,min('$maxwidth'/iw\,'$maxheight'/ih))/2)*2:-2" -c:v libx264 '$fps_option' -crf 28 -c:a aac '$subtitle_option' -b:v '$bitrate' -maxrate '$bitrate' -bufsize '$bufsize' -f '$container' "'"$output"'"' 
-        eval $cmd+        $binary -i "$input" -filter_complex "scale=ceil(iw*min(1\,min($maxwidth/iw\,$maxheight/ih))/2)*2:-2" -c:v libx264 $fps_option -crf 28 -c:a aac $subtitle_option -b:v $bitrate -maxrate $bitrate -bufsize $bufsize -f $container "$output"
         if [ $? -eq 0 ];         if [ $? -eq 0 ];
         then         then
             actualheight=$(ffprobe -v error -select_streams v -show_entries stream=height -of csv=p=0:s=x "$output")             actualheight=$(ffprobe -v error -select_streams v -show_entries stream=height -of csv=p=0:s=x "$output")
             #output2="${output%.*}."$actualheight"p.$container"             #output2="${output%.*}."$actualheight"p.$container"
-            output2="${output//.convertednew./.$actualheight""p.}"+            output2="${output//.convertednew/.$actualheight""p.}$container"
             mv "$output" "$output2"             mv "$output" "$output2"
             echo "Output file: $output2"             echo "Output file: $output2"
Line 121: Line 155:
     fi     fi
 done done
 +
 +# When using in Nemo, it's helpful to force a key to be pressed before closing the terminal to see the status or any errors. Uncomment the following line:
 +
 +#echo;read -rsn1 -p "Press any key to continue . . .";echo
 </code> </code>
  
Line 131: Line 169:
 Icon-Name=stock_down Icon-Name=stock_down
 Selection=notnone Selection=notnone
-Extensions=mp4;wmv;avi;mkv;mov+Extensions=mp4;wmv;avi;mkv;mov;webm;mpg
 Quote=double Quote=double
 EscapeSpaces=true EscapeSpaces=true
Line 140: Line 178:
 <code> <code>
 echo;read -rsn1 -p "Press any key to continue . . .";echo echo;read -rsn1 -p "Press any key to continue . . .";echo
 +</code>
 +
 +===== Reencode videos with high bitrate =====
 +
 +<code bash processfiles.sh>
 +#!/bin/bash
 +MYFILES=$(find /media/videofiles -type f -iname "*.mp4")
 +SAVEIFS=$IFS
 +
 +IFS=$(echo -en "\n\b")
 +for FILE in ${MYFILES}
 +do
 +  bitrate=$(ffprobe -v quiet - select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 $FILE)
 +  if ! [[ $bitrate =~ ^[0-9]+$ ]];
 +  then
 +      continue
 +  fi
 +  if [ $bitrate -gt 8000000 ]
 +  then
 +      echo $bitrate" | "$FILE
 +      video_downscale.sh "$FILE"
 +  fi
 +done
 +IFS=$SAVEIFS
 +</code>
 +
 +
 +===== 3D VR SBS to 2D =====
 +
 +https://blog.interstellar.co.jp/en/2022/06/21/converting-vr180-videos-to-2d-videos-with-ffmpeg/
 +
 +<code>
 +ffmpeg -i input.mp4 -vf v360=input=equirect:output=flat:ih_fov=180:iv_fov=180:h_fov=93:v_fov=121:in_stereo=sbs:w=960:h=640 -codec:v h264 output.mp4
 +</code>
 +
 +https://video.stackexchange.com/questions/21084/how-to-convert-a-3d-movie-to-2d-using-ffmpeg
 +
 +<code>
 +ffmpeg -i input.mkv -vf stereo3d=sbsl:ml -metadata:s:v:0 stereo_mode="mono" output.mkv
 +</code>
 +Use sbsl2:ml or  -aspect 16:9 if the aspect ratio is wrong
 +
 +
 +https://stackoverflow.com/questions/66960003/unwarping-180-vr-footage-with-ffmpeg-v360-filter
 +
 +
 +https://github.com/paulpaul999/vr-video-notes/blob/main/vr-to-flat/README.md
 +<code>
 +ffmpeg -i equirectangular.mp4 -filter:v "v360=input=hequirect:output=flat:in_stereo=sbs:out_stereo=2d:d_fov=125:w=1920:h=1080:pitch=+5" -map 0 -c copy -c:v h264 -pix_fmt yuv420p flat.mp4
 </code> </code>
howto/convert-images.1708975096.txt.gz · Last modified: 2024/02/26 19:18 by Wulf Rajek