Jellyfin Forum
Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - Printable Version

+- Jellyfin Forum (https://forum.jellyfin.org)
+-- Forum: Off Topic (https://forum.jellyfin.org/f-off-topic)
+--- Forum: General Discussion (https://forum.jellyfin.org/f-general-discussion)
+--- Thread: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) (/t-encoding-discussion-megathread-ffmpeg-handbrake-av1-etc)

Pages: 1 2 3 4 5 6 7 8 9 10 11


RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - TheDreadPirate - 2023-08-27

I haven't had a chance to look into why the crop variable was always null. I was focused on chapters yesterday because my current batch of rips pending recompression are 4:3 DVDs (no cropping necessary), 1 MKV per disc of 4-5 episodes.

I have a couple 21:9 blurays coming up so I will be looking into getting cropping working tomorrow.


RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - bitmap - 2023-08-27

(2023-08-26, 07:36 PM)TheDreadPirate Wrote: Do note that I am using jellyfin-ffmpeg.  I didn't want to install vanilla ffmpeg and risk breaking something.

Just as a note (I missed this before), they can co-exist happily. I ran both for some testing I was running before I upgraded my server. Well... The version number went up. Encoding performance went the other direction...

As for de-interlacing, I'm not sure that's something you can turn off server side unless you disable transcoding. So if the client supports it, not a big deal. But if it doesn't, you're looking at a transcode.

As for the command, I'm pretty sure it's just another video filter. I'm away from my PC (on mobile) but if remember right it's something like -vf yadif=mode where mode is drop frame or double frame (recommend double frame). This is one of the de-interlacing options in the Jellyfin dashboard actually.

I'll try to check today and revise.


RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - bitmap - 2023-08-27

(2023-08-27, 05:17 AM)TheDreadPirate Wrote:
Code:
#!/bin/bash

chaptersEpisode=$1

if [ -z "$chaptersEpisode" ]; then
        echo "Chapters per episode was not provided"
        exit 1
fi

ls *.mkv | while read file; do
        sudo /usr/lib/jellyfin-ffmpeg/ffprobe -i $file -print_format json -show_chapters -loglevel error > chapters.json
        rawChapters=$(jq .chapters[].start_time chapters.json | wc -l)
        totalChapters=$(echo $(($rawChapters - 1)) | bc)
        i=0
        startStop=""
        if [ "$chaptersEpisode" == "all" ]; then
                startStop="0:$totalChapters"
        else
                while [ $i -lt $totalChapters ]; do
                        first=$i
                        last=$(echo $(($i + $chaptersEpisode - 1)) | bc)
                        startStop=$(echo "$startStop $first:$last")
                        i=$(echo $(($i + $chaptersEpisode)) | bc)
                done
        fi
        for chapter in `echo $startStop` ; do
                begin=$(echo $chapter | awk -F ':' '{print $1}')
                end=$(echo $chapter | awk -F ':' '{print $2}')
                startTime=$(jq .chapters[$begin].start_time chapters.json | sed 's/\"//g')
                endTime=$(jq .chapters[$end].start_time chapters.json | sed 's/\"//g')
                sudo /usr/lib/jellyfin-ffmpeg/ffmpeg -init_hw_device vaapi=va:,driver=iHD,kernel_driver=i915 \
                -init_hw_device qsv=qs@va -filter_hw_device qs -hwaccel qsv -hwaccel_output_format qsv \
                -i $file -map 0:v -map 0:a -map 0:s -map_chapters 0 -c:v hevc_qsv -profile main \
                -preset slow -cq 26 -pix_fmt yuv420p -vf yadif=1 -c:a copy -c:s copy -ss $startTime -to $endTime "$file-chapter-$begin-$end.mkv"
        done
done

All I did here was add de-interlacing with yadif; the recommended mode is 1, which is "send field" and will double the framerate for content. This can be problematic for films, so you can run mode 0 (which is one frame only and should preserve framerate). You can also try the auto leveling filter if you're interested, but I've never played with it before and my understanding is that what it does is just stretch luminance levels in the input media. Some folks also recommend decomb when ripping DVDs (which is how Handbrake generally recommends doing deinterlacing).


RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - TheDreadPirate - 2023-08-27

(2023-08-27, 10:50 PM)bitmap Wrote: All I did here was add de-interlacing with yadif; the recommended mode is 1, which is "send field" and will double the framerate for content. This can be problematic for films, so you can run mode 0 (which is one frame only and should preserve framerate). You can also try the auto leveling filter if you're interested, but I've never played with it before and my understanding is that what it does is just stretch luminance levels in the input media. Some folks also recommend decomb when ripping DVDs (which is how Handbrake generally recommends doing deinterlacing).

Thanks a bunch!  I will probably add more options and rework it a bit.  After tonight, I'm off the rest of the week so I'll have plenty of time.

1)  Instead of reading the current directory from within the script, require the file name be passed in.  Allows for something like a find command to work through a nested folder structure and to place the output within that nested folder structure.
2)  Add logic to switch deinterlacing on and off.  Probably based on the output from ffprobe.
3)  Get dynamic cropping working.


RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - bitmap - 2023-08-27

Reading more, handbrake uses decomb in place of yadif (which may call yadif, but may also use other de-interlacing filters). So I'd stick with yadif. Takes longer, but should do a better job. Good luck, definitely share what you get out of it. My bash skills are rough and refine over time but if you create something pretty and useful, it'd be great to share out. I feel like handbrake is great but not as flexible.


RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - TheDreadPirate - 2023-08-30

@nyanmisaka

What is the syntax for having both deinterlacing and crop filters? Right now whichever is last in the command gets run (currently crop).

Quote:Multiple -filter, -af or -vf options specified for stream 0, only the last option '-filter:v crop=w=716:h=480:x=0:y=0' will be used.

An example deinterlace parameter

-vf yadif=1

My crop parameter

-vf crop=w=1920:h=848:x=0:y=116"

How would they look together, if possible?


RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - bitmap - 2023-08-30

This might be where -filter_complex comes in?

I'm dumb. You should just need to follow the syntax I had before...

Code:
-vf "filterA=option:value, filterB=option:value"

You need quotes, you need to separate the filters with a comma. You shouldn't need anything else. Audio and video filters are invoked separately.

EDIT: I'll just do what you asked instead of being abstract about it...

Quote:An example deinterlace parameter

-vf yadif=1

My crop parameter

-vf crop=w=1920:h=848:x=0:y=116"

How would they look together, if possible?

Code:
-vf "crop=w=1920:h=848:x=0:y=116, yadif=1"

Order matters. I think you would want to crop first, then deinterlace, since it's less video to deinterlace, though since it's just black bars it's probably a wash.


RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - TheDreadPirate - 2023-08-31

This worked. Also, yadif is waaaay better looking than decomb.

There were a couple Animes where there was noticeable deinterlacing artifacts with decomb. I might go back and redo the encode with this script and yadif.


RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - grunt - 2023-08-31

I am enjoying reading this discussion and if anyone is interested would like to share the scripts I use to convert files to 720/1080 HEVC for things I dont really care about (like wife's reality tv...)

These two scripts will look for supported file types and then convert them to the following settings, I could make it more precise like OPs (look for mkv or mp4 or whatever only) but this works for me well enough.

Also this is not using FFMPEG, but a custom packaged version of handbrake-CLI with intel gpu drivers built in (for *unix distro) 

Code:
#!/bin/bash

#folder where files area located to be converted
rootfolder="*******"
#folder where file will be save
savefolder="*********"

echo "#####################################"
echo "Files must be located in $rootfolder"
echo "Supported file types in folder will be converted to 720p HEVC 10bit @ 1000kbps, with a CFR value of 21 using opus audio codec at 80kbps including all subtitles in the mkv file format"
echo "If file or file type is not supported it will just be skipped"
echo "After completion, all files will be located in the $savefolder"
echo "#####################################"


#Handbrake using qsv 10bit hevc with 80kbs OPUS stereo audio including all subtitles


for file in "$rootfolder"*
do
  echo -e "The following file will be converted \e[36m$(basename "$file")\e[m"
  final="$(basename "$file")"
  finalclean="${final%.*}"
  save="$savefolder$finalclean-mau5.mkv"
  echo -e "File will be saved as \e[35m$finalclean-mau5.mkv\e[m and saved to \e[36m$savefolder\e[m"
  sleep 5
  flatpak run --command=HandBrakeCLI fr.handbrake.ghb --enable-qsv-decoding --encoder qsv_h265_10bit --width 1280 --height 720p --vb 1000 --no-multi-pass --vfr --cfr 21 --format av_mkv --aencoder opus --ab 80 --mixdown stereo --all-subtitles --input "$file" --output "$save"
done

#echo -e "finish converting \e[35m$save\e[m"

Code:
#folder where files area located to be converted
rootfolder="******************"
#folder where file will be save
savefolder="*****************"

echo "#####################################"
echo "Files must be located in $rootfolder"
echo "Supported file types in folder will be converted to 1080p HEVC 10bit @ 2500kbps, with a CFR value of 23 using 5.1 OPUS audio codec at 160kbps including all subtitles in the mkv file format"
echo "If file or file type is not supported it will just be skipped"
echo "After completion, all files will be located in the $savefolder"
echo "#####################################"


#Handbrake using qsv 10bit hevc with 80kbs OPUS stereo audio including all subtitles


for file in "$rootfolder"*
do
  echo -e "The following file will be converted \e[36m$(basename "$file")\e[m"
  final="$(basename "$file")"
  finalclean="${final%.*}"
  save="$savefolder$finalclean-mau5.mkv"
  echo -e "File will be saved as \e[35m$finalclean-mau5.mkv\e[m and saved to \e[36m$savefolder\e[m"
  sleep 5
  flatpak run --command=HandBrakeCLI fr.handbrake.ghb --enable-qsv-decoding --encoder qsv_h265_10bit --width 1920 --height 1080 --vb 2500 --no-multi-pass --vfr --cfr 21 --format av_mkv --aencoder opus --ab 160 --mixdown 5point1 --all-subtitles --input "$file" --output "$save"
done

#echo -e "finish converting \e[35m$save\e[m"


I also have a script where you can input whatever you want as values (for when I want to do anything custom)

Code:
#folder where files area located to be converted
rootfolder="/mnt/Chun-Li/._convert/"
#folder where file will be save
savefolder="/mnt/Chun-Li/._converted/"

echo "#####################################"
echo "Supported file types in folder will be converted to whatever audio/video options specified with all included subtitles"
echo "If file or file type is not supported it will just be skipped"
echo "After completion, all files will be located in the $savefolder"
echo "#####################################"
echo -e "\e[31mVideo options\e[m"
echo " "
# width="USER INPUT"
read -p "Width of the video? " width
echo -e "You entered \e[32m$width\e[m"
# height="USER INPUT"
read -p "Height of the video? " height
echo -e "You entered \e[32m$height\e[m"
# videobitrate="USER INPUT"
read -p "What video bitrate (in kbps)? " videobitrate
echo -e "You entered \e[32m$videobitrate\e[m"
# cfr="USER INPUT"
read -p "What CFR value (21 default, RF 19-23 for 720p, RF 20-24 for 1080p, RF 22-28 for 2160p)? " cfr
echo -e "You entered \e[32m$cfr\e[m"
echo "###################################"
echo -e "\e[31mAudio Options\e[m"
echo " "
# audiocodec="USER INPUT"
read -p "What audio codec (opus, av_aac, ac3, eac3, copy:)? " audiocodec
echo -e "You entered \e[32m$audiocodec\e[m"
# audiobitrate="USER INPUT"
read -p "What audio bitrate (in kbps)? " audiobitrate
echo -e "You entered \e[32m$audiobitrate\e[m"
# audioformat="USER INPUT"
read -p "How many channel audio? (mono, stereo, dpl1, dpl2, 5point1, 7point1)? " audioformat
echo -e "You entered \e[32m$audioformat\e[m"
echo "###################################"
echo ""
echo -e "All files saved to \e[36m$savefolder\e[m"
echo -e "All files will be converted to mkv"

#Handbrake using qsv 10bit hevc with 80kbs OPUS stereo audio including all subtitles


for file in "$rootfolder"*
do
  echo -e "The following file will be converted \e[36m$(basename "$file")\e[m"
  final="$(basename "$file")"
  finalclean="${final%.*}"
  save="$savefolder$finalclean-mau5.mkv"
  echo " "
  echo -e "File will be saved as \e[35m$finalclean-mau5.mkv\e[m and saved to \e[36m$savefolder\e[m"
  sleep 5
  flatpak run --command=HandBrakeCLI fr.handbrake.ghb --enable-qsv-decoding --encoder qsv_h265_10bit --width "$width" --height "$height" --vb "$videobitrate" --cfr "$cfr" --no-multi-pass --vfr --format av_mkv --aencoder "$audiocodec" --ab "$audiobitrate" --mixdown "$audioformat" --all-subtitles --input "$file" --output "$save"
  echo " "
  echo -e "Finished converting \e[35m$finalclean\e[m with a resolution of \e[32m$width\e[m x \e[32m$height\e[m with a bitrate of \e[32m$videobitrate\e[mkbps using the \e[32m$audiocodec\e[m audio codec at a bitrate of \e[32m$audiobitrate\e[mkbps in \e[32m$audioformat\e[m sound to folder \e[35m$savefolder\e[m."
  echo " "
  echo "###################################"
  echo " "
done

#echo -e "finish converting \e[35m$save\e[m"



RE: Encoding Discussion Megathread (ffmpeg, Handbrake, AV1, etc...) - bitmap - 2023-08-31

Hahaha, "more precise like OP's"! I love that! I'm running my stuff in the background (a precious show to me that I have in 4K HDR that took me ages to hunt down) and the compression to visual quality I'm getting with AV1 is astouding. The only issue is I'm wondering what goddamn file is being converted because ab-av1 doesn't tell you anything.

Well if I weren't a ding-dong, I'd use echo. Like you. And maybe suppress the output from ffmpeg because it's so flippin' noisy with "HEY DID YOU KNOW I WAS COMPILED WITH ALL THESE LIBS AND HEY DID YOU ALSO KNOW THAT I CAN TELL YOU ABOUT THE FILE THAT YOU MAPPED PRECISELY BEFORE UNDERTAKING THIS CONVERSION AND HEY DID YOU ALSO KNOW..." It's like I need a pillow and a flexible sense of morality...